Click here to Skip to main content
16,001,934 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
plz tell me how i can change the image on canvass, i have 2 canvas on a single webpage, how i change the image on clicking the button . both canvas fill with image ????
Posted
Comments
What have you tried?
Member 10542187 6-Apr-14 11:15am    
i have tried this but i can`t understnd where i put the code related to change image ??????????




<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<canvas id="myCanvas" width="578" height="400" aign ="left" style="border:1px solid #d3d3d3;"></canvas>
<canvas id="myCanvas1" width="578" height="400" align="right" style="border:1px solid #d3d3d3;"></canvas>

<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var imageObj = new Image();
var canvas = document.getElementById('myCanvas1');
var context1 = canvas.getContext('2d');
var imageObj1 = new Image();



imageObj.onload = function() {
context.drawImage(imageObj, 69, 50);
context1.drawImage(imageObj1, 69, 50);

};
imageObj.src = 'images/a.jpg';
imageObj1.src = 'images/b.jpg';

</script>
</body>
</html>
Write one button click event and write inside that for changing the image.
Member 10542187 6-Apr-14 11:22am    
what i write inside that button ???
any function ???
Add one click event for the button like...

<input type="button" id="btnSomeId" onclick="someFunction()" />

Inside the function do what you wish.

1 solution

Hi
It seems that this code was patch together from pieces of code. You are calling var canvas twice and are try to print images all together. If you want to better understand how things work, please have a look at:
http://www.w3schools.com/html/html5_canvas.asp[^]
http://www.w3schools.com/js/js_functions.asp[^]
Start from the basic and work your way up.

However, if you feel like you can do this starting from half way, have a look on this Tip/Trick
Image Sliding Effect with JavaScript[^] and adapt it to clicking instead of the timer. If it looks to difficult to do it, then maybe you should look and practice at the first 2 links.

Good Luck

Regards
 
Share this answer
 

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