admin管理员组文章数量:1336144
I am making a drawing board in canvas. I am trying to change the background of the canvas when a input image is clicked using jquery.
This is my html code for the canvas and the button
Canvas:
<canvas id="myCanvas" width="640" height="480"></canvas>
Input type image on click:
<section id="canvascolor">
<input id="whitecanvas" class="canvasborder" type="image" src="images/whitecanvas.jpg" onClick="Whitecanvas()">
<input id="blackcanvas" class="canvasborder" type="image" src="images/blackcanvas.jpg" onClick="Blackcanvas()">
<input id="yellowcanvas" class="canvasborder" type="image" src="images/yellowcanvas.jpg" onClick="Yellowcanvas()">
<input id="bluecanvas" class="canvasborder" type="image" src="images/bluecanvas.jpg" onClick="Bluecanvas()">
<input id="redcanvas" class="canvasborder" type="image" src="images/redcanvas.jpg" onClick="Redcanvas()">
<input id="greencanvas" class="canvasborder" type="image" src="images/greencanvas.jpg" onClick="Greencanvas()">
</section>
CSS:
canvas{
background-image:url(../images/whitetexturepaper.jpg);
background-repeat:no-repeat;
background-size:cover;
}
I have tried the following jquery but it doesnt work!:
function Whitecanvas(){
$("#myCanvas").css("background-image","url(images/whitetexturepaper.jpg)");
}
I am making a drawing board in canvas. I am trying to change the background of the canvas when a input image is clicked using jquery.
This is my html code for the canvas and the button
Canvas:
<canvas id="myCanvas" width="640" height="480"></canvas>
Input type image on click:
<section id="canvascolor">
<input id="whitecanvas" class="canvasborder" type="image" src="images/whitecanvas.jpg" onClick="Whitecanvas()">
<input id="blackcanvas" class="canvasborder" type="image" src="images/blackcanvas.jpg" onClick="Blackcanvas()">
<input id="yellowcanvas" class="canvasborder" type="image" src="images/yellowcanvas.jpg" onClick="Yellowcanvas()">
<input id="bluecanvas" class="canvasborder" type="image" src="images/bluecanvas.jpg" onClick="Bluecanvas()">
<input id="redcanvas" class="canvasborder" type="image" src="images/redcanvas.jpg" onClick="Redcanvas()">
<input id="greencanvas" class="canvasborder" type="image" src="images/greencanvas.jpg" onClick="Greencanvas()">
</section>
CSS:
canvas{
background-image:url(../images/whitetexturepaper.jpg);
background-repeat:no-repeat;
background-size:cover;
}
I have tried the following jquery but it doesnt work!:
function Whitecanvas(){
$("#myCanvas").css("background-image","url(images/whitetexturepaper.jpg)");
}
Share
Improve this question
edited Nov 9, 2013 at 16:04
asked Nov 9, 2013 at 15:58
user2953775user2953775
1
- Since you will let the user draw on that image, I suggest you use 2 elements instead of a single canvas with a background-image. Create a background img element with a foreground canvas element absolutely positioned directly on top of that image. The background image will show through the canvas. You will increase drawing performance because you don't have to redraw the image each time the user changes their drawing. – markE Commented Nov 9, 2013 at 17:32
2 Answers
Reset to default 3Put your code at the end after element is defined .
Fiddle DEMO
problem you are defining function before element is in DOM.
Better use jQuery Version
remove onclick events from input tags in section
Fiddle DEMO
$(document).ready(function () {
$("#canvascolor > input").click(function () {
var img = $(this).attr('src');
$('#myCanvas').css("background-image", "url(" + img + ")");
});
});
Try to use only one ID
function Whitecanvas() {
$("#whitecanvas").css("background-image", "url(images/whitetexturepaper.jpg)");
}
本文标签:
版权声明:本文标题:javascript - How to change the canvas background image when a input type button is clicked using jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742395240a2466789.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论