I am making a layered image of sorts for the header on a website. This is going to have individual components that animate in various simple ways, and some require blending modes, so I will obviously have to use HTML5 Canvas.
I did some research on how to insert an image, and I found this:
$(document).ready(function(){
var canvas = document.getElementById("myCanvasName");
var ctx = canvas.getContext("2d");
var image = document.getElementById("myImageToInsert");
canvas.height = image.offsetHeight;
canvas.width = image.offsetWidth; //to set the canvas's dimensions, to allow for a responsive design
ctx.drawImage(base,0,0);
});
Pretty simple, I grab the HTML <img>
tag, and include it in the canvas. The problem is that now I have two images in my markup: the canvas and the <img>
tag the canvas is drawing from. This is obviously undesirable.
If I try to set display:none
on the <img>
it affects the canvas element as well (and causes it to not display the image).
My question: How can I either import an image into Canvas without using markup, or how can I hide the source image?
Here's a fiddle, you can see that there are two images (one canvas) and display none will affect both of them. (Even when it's only called on the image tag): http://jsfiddle.net/9z9x1gb7/