简体   繁体   English

更改HTML5画布中的背景图像

[英]Change background image in HTML5 canvas

I need to change background image in HTML5 canvas. 我需要在HTML5画布中更改背景图片。

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. 我想使用2个不同的缩略图作为按钮,当我单击其中一张图像时,背景应更改为该特定图像。

Technique 1 技术1

  1. On click of the appropriate buttons set a unique CSS class on your <canvas> element. click适当的按钮后,在<canvas>元素上设置唯一的CSS类。
  2. Use CSS to apply a background image to your canvas. 使用CSS将背景图片应用于画布。

Drawing commands and erasing your canvas will not affect the CSS-applied background image behind your content. 绘制命令和擦除画布不会影响内容后面的CSS应用背景图像。

Technique 2 技术2

  1. 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. 编写一个redraw()函数,该函数知道如何擦除画布并重新生成图形,包括首先确定要绘制的图像作为背景。
  2. On click of the buttons set a variable and re-invoke your redraw() function. click按钮后,设置一个变量并重新调用您的redraw()函数。 It will erase your canvas and use the variable to draw the correct image. 它将擦除您的画布,并使用该变量绘制正确的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM