简体   繁体   English

如何将图像放置在画布后面

[英]How to place the image, behind the Canvas

I'm in the final stretch of the game and when I put the background image, it overlaps the Canvas, how do I fix this?我在游戏的最后阶段,当我放置背景图像时,它与画布重叠,我该如何解决这个问题? - Google translator -谷歌翻译

game游戏

game with background有背景的游戏

将图像作为包含画布的元素的 css 背景图像,或者使用比画布小的 z-index 值使图像成为绝对图像

You can make use of drawImage property of canvas您可以利用画布的drawImage属性

 var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); var background = new Image(); background.src = "https://cdn0.iconfinder.com/data/icons/is_google_plus_one_icons/256/google_plus_one_-plus-1_canvas.png"; background.onload = function(){ ctx.drawImage(background,0,0, 200, 100); ctx.moveTo(0,0); ctx.lineTo(200,100); ctx.stroke(); }
 <canvas id="myCanvas" width="200" height="100" style="border:1px solid #d3d3d3;">

Make use of onload property of image to draw the canvas contents.利用图像的onload属性绘制画布内容。

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

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