Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Js fiddle[]

How can I set an image into this ctx? As you see it has only colors as fillstyle. But I would like to put image over there.

What should I do?

What I have tried:

var bigGreen = new BigCircle(ctx,50, 50, '#5eb62b', 50);

I tried put image url to function argument:

var bigGreen = new BigCircle(ctx,50, 50, 'url("myimage.png")', 50);

Not working.
Posted
Updated 21-Mar-16 17:49pm

1 solution

<html>
<body>
<canvas id="canvas" width="500" height="100">
Canvas not supportet!
</canvas>
</body>
</html>

BigCircle = function(ctx,x, y, color, circleSize) {
ctx.beginPath();
ctx.arc(x, y, circleSize, 0, Math.PI * 2, true);
ctx.fillStyle=color
ctx.fill();
ctx.closePath();
this.clicked=function(){
ctx.fillStyle='#ff0000'
ctx.fill();
}
};

canvas.width = 903;
canvas.height = 657;

var background = new Image();
background.src = "http://i.imgur.com/yf6d9SX.jpg";

function init() {
var canvas = document.getElementsByTagName("canvas")[0];
var ctx = canvas.getContext('2d');
var bigGreen = new BigCircle(ctx,50, 50, '#5eb62b', 50);
$('#canvas').click(function(e){
var x = e.clientX
, y = e.clientY
if(Math.pow(x-50,2)+Math.pow(y-50,2) < Math.pow(50,2))
bigGreen.clicked()
ctx.drawImage(background,0,0);
})
}


$(document).ready(function() {
init();
});
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900