问题
I need to change background image in HTML5 canvas.
I want to use 2 different thumbnails as buttons and when I click on one of the images the background should be change to that particular image.
回答1:
Technique 1
- On
clickof the appropriate buttons set a unique CSS class on your<canvas>element. - Use CSS to apply a background image to your canvas.
Drawing commands and erasing your canvas will not affect the CSS-applied background image behind your content.
Technique 2
- Write a
redraw()function that knows how to erase the canvas and regenerate your drawing, including determining what image to draw as the background first. - On
clickof the buttons set a variable and re-invoke yourredraw()function. It will erase your canvas and use the variable to draw the correct image.
来源:https://stackoverflow.com/questions/9688173/change-background-image-in-html5-canvas