简体   繁体   English

html5画布背景img

[英]html5 canvas background img

when i write a img to the back ground of a canvas it works but if i refresh it disapears please help this is a really annoying bug in my page. 当我在画布的背景上写一张img时,它可以工作,但是如果我刷新它,它就会消失,请帮忙,这是我页面中一个非常烦人的错误。 heres a sample bit of code that is doing it 这是执行此操作的示例代码

<img id="back" src="back.jpg" alt="background" width="200" height="300">
<canvas id="myCanvas" width="300" height="400" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script>

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var img=document.getElementById("back");
ctx.drawImage(img,0,0);


ctx.moveTo(20,375);
ctx.lineTo(150,375);
ctx.stroke();

ctx.moveTo(70,375);
ctx.lineTo(70,50);
ctx.stroke();

ctx.moveTo(70,120);
ctx.lineTo(130,50);
ctx.stroke();

ctx.moveTo(70,50);
ctx.lineTo(180,50);
ctx.stroke();

ctx.moveTo(180,50);
ctx.lineTo(180,100);
ctx.stroke();


</script>

By the time your script executes, the image might not yet be loaded. 在脚本执行时,图像可能尚未加载。 Run your drawing code from the onload event of the image to make sure it happens only after it's been loaded. 从图像的onload事件运行您的绘图代码,以确保它仅在加载后才发生。

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

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