What is wrong with my drawImage function? - javascript

I cant seem to get my drawimage function to work on my canvas. I tried using the onload method I tried using the file on the application. Nothing seems to work, however. All I want to do is print the image from the url into my canvas.

Change line 5 to let image = document.createElement("img");
When drawing images to a canvas you must provide an image element, created with document.createElement or stored in your index.html

Related

Export canvas on fabric.js to an image

I'm using fabric.js to make a draw and export the canvas to a .png without background. The thing is that I've been searching and this is the only thing that, in a way, makes sense to me:
document.getElementById("downloadbtn").onclick = saveImage();
function saveImage(c) {
this.href = canvas.toDataURL({
format: 'png',
quality: 0.8
});
this.download = 'testimage.png'
}
But it doesn't work... I've also tried a bunch of different stuff but the same thing happens:
Failed to execute toDataURL... error in the console.
Any helps? Ty
For those who had the same issue as me, the thing is:
You can save easily the canvas to an image, but you can't do it if you have an image inside the canvas, let me explain. If you have imported a nonlocal image into the canvas (like pasting an image with fromURL), the CORS of the web will detect this as a probable threat for the system, and will block the action.
For me, saving the canvas without the url image is fine, so I have no other solution for those who try to save the canvas with an external image, but maybe save first locally the image and then save it will work for you.
Hope it can help!

Is it more memory efficent to load an image into a canvas or using URL.convertToDataURL to apply the src

Is it more beneficial to use a canvas element as a buffer for the image or is it more beneficial to use an img element as a buffer?
is the closest thing to what I'm asking except the use case is slightly different.
Currently I receive a file, for example:
let file=document.getElementById("input").files[0];
Then using URL.createObjectURL(file); it can be injected into the src attribute of an <img />.
My feeling is that using a canvas instead and drawing from the the file blob, would use less memory. Except it seems that in most examples an image is still used as a go-between.
Ex.
let image = document.getElementById("image");
let canvas = document.getElementById("canvas");
canvas.drawImage(image,0,0);
image.parentNode.deleteChild(image);
In this example I'm using DOM which is slow (not that speed matters in this case) I would likely use a temporary image object like everyone else.
The question is if going the extra step and putting the data into the canvas would be at all ever beneficial or does the browser do some kind of optimization where the src blob url is minimized or removed in some way to save memory?
As a sub question if I did want it in a canvas after all to do image processing or something is there a more efficient way of getting a file into a canvas?

Capture/save/export an image with CSS filter effects applied

I'm tooling around to make a simple picture editor that uses CSS3 filter effects (saturation, sepia, contrast, etc.)
Making the picture editor is the easy part, however whether it is possible to save or export the image with the filters applied seems incredibly difficult..
I had originally had high hopes it would be possible with #niklasvh's html2canvas. Unfortunately, it doesn't capture most CSS3 properties, let alone filter effects.
If anybody has a solution or sadly, definitive knowledge that this just isn't possible, it would be greatly appreciated! Thanks!
You're not, that I'm aware of, able to apply CSS to graphics in the HTML5 canvas element (as they're not a part of the DOM).
However, that's OK! We can still do basic filter effects relatively easy and save them out as an image with just a few lines of JavaScript.
I found a good article that goes over applying a sepia-like effect to the canvas and saving it as an image. Rather than copying it, I'll highlight the larger takeaways from the article.
Modifying the canvas image:
var canvas = document.getElementById('canvasElementId'),
context = canvas.getContext('2d');
var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
for (var i = 0; i < imageData.data.length; i+=4) {
...
}
In order to get access to some canvas API, you'll need to activate the 2d context on the canvas using the above JavaScript. MDN has some great documentation on the API that is available to you with the context object, but the important part to note here is the getImageData() call. Basically, it will grab all the pixel values in the area that you defined (in the case above, we're grabbing the whole image). Then, with this data in hand, we can iterate through all the pixels and change them as needed. In the sepia article, it's obviously making some interesting adjustments, but you can also do grayscale, blurring, or any other changes as necessary and there's an awesome canvas filters library on Github for just that.
How to save the canvas image:
var canvas = document.getElementById('canvasElementId'),
image = document.createElement("img");
image.src = canvas.toDataURL('image/jpeg');
document.body.appendChild(image);
The above script will select your canvas (assuming you've already done your drawings) and create an image element. It them uses toDataURL() to generate a url that you can use to set the source on an image element. In the example above, the image element is appended to the document body. You can view more info on MDN's canvas page.
I got your answer.
I made this program, finally it's work.
those step is :
1. upload the image (JPG/PNG)
2. convert to canvas
3. custom with css filters.
4. render using camanJS to save as image.
5. done.
you also can reset effect value by modifying value of filters to its default.
good luck!

drawing image from localhost onto canvas in chrome

I am accessing an image from a folder in localhost.
Image loads correctly in img tag. But, when I try to draw that image onto canvas, it doesn't appear!
I have implemented it such that when image is dropped onto the canvas, it is drawn onto it and I draw it by getting the image from original source. When I drag again and again, then, after some attempts, it appears.
The functionality is working perfectly in Firefox and I.E.
This is the source of my image on localhost:
b.src = "http://localhost/casema...E/2780Chrysanthemum.jpg"
ctx1.drawImage(b, x2-15*z, y2-15*z, w1, h1 );
In HTML5, it not for sure that an image will always load immediately, so you
need to make sure that the image has fully loaded beford you draw it. this code may help you :
var myImage = new Image();
myImage.src = "path/to/your/image";
myImage.onload = function(){
//context is the canvas context
context.drawImage(myImage,x,y,weight,height);
}
Are you using a local HTML file on your computer? If so you need to add the parameter
--allow-file-access-from-files
when calling Chrome.

save image to a png file using processing.js

I have an small drawing application made in processing.js. I would like to save the image made using the application to a server using a php file (no problem on this).
I know the canvas element has a method to get that content, canvas.toDataURL() but I dont know how to do the same with processing.js
Any hint?
Cheers!
perhaps
var image = document.getElementsByTagName("canvas")[0].toDataURL();
So when you use saveFrame() in processing.js. The image opens in a new window and i found it hard to intercept the data. The soultion is to get the image from the canvas in javascript.
// this grabs the canvas and turns it into a base64 image
var image = document.getElementsByTagName("canvas")[0].toDataURL();
// Log the data to the canvas to check it working
console.log(image);
// then you can place the image on your web page
document.getElementById("theImage").src=image;
Then the html is easy. Just add
<img id="theImage"></img>
If you want to upload the image to a server you can acsess the data in js with.
image
Note that you can also use style display:none; and have the canvas render image for you without the canvas displaying. It also supports transparent pngs and the canvas by default is transparent

Categories