Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript html5画布背景img_Javascript_Css_Html_Canvas - Fatal编程技术网

Javascript html5画布背景img

Javascript html5画布背景img,javascript,css,html,canvas,Javascript,Css,Html,Canvas,当我在画布的背景上写一个img时,它可以工作,但是如果我刷新它,它就会消失,请帮助我,这是我页面中一个非常恼人的bug。下面是一段代码示例 <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

当我在画布的背景上写一个img时,它可以工作,但是如果我刷新它,它就会消失,请帮助我,这是我页面中一个非常恼人的bug。下面是一段代码示例

<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>

您的浏览器不支持HTML5画布标记。
var c=document.getElementById(“myCanvas”);
var ctx=c.getContext(“2d”);
var img=document.getElementById(“返回”);
ctx.drawImage(img,0,0);
ctx.moveTo(20375);
ctx.lineTo(150375);
ctx.stroke();
ctx.moveTo(70375);
ctx.lineTo(70,50);
ctx.stroke();
ctx.moveTo(70120);
ctx.lineTo(130,50);
ctx.stroke();
ctx.moveTo(70,50);
ctx.lineTo(180,50);
ctx.stroke();
ctx.moveTo(180,50);
ctx.lineTo(180100);
ctx.stroke();

在脚本执行时,图像可能尚未加载。从图像的
onload
事件中运行图形代码,以确保只有在加载后才会发生