简体   繁体   English

用JavaScript显示图像

[英]Image Displaying in JavaScript

I am trying to make a clip art image appear inside of a canvas in JavaScript and for some reason the clip art is not appearing on top of the rectangle I am drawing as a border for it and I have no clue how to make them both appear. 我试图使剪贴画图像出现在JavaScript的画布内,由于某种原因,剪贴画没有出现在矩形的上方,因此我正在为其绘制边框,而且我不知道如何使它们都出现。 The rectangle is showing up just not the picture. 矩形只显示而不是图片。 Am I not allowed to put both in a canvas? 我不能把两者都放在画布上吗? Do I need to layer two canvases? 我需要两个画布吗? Am I just coding this incorrectly? 我只是编码不正确吗? Any help would be much appreciated. 任何帮助将非常感激。

<canvas id = "myCanvas" width = "400" height="400"></canvas>
<script>
var r = document.getElementById("myCanvas");
var rec = r.getContext("2d");
rec.strokeStyle = "black";
rec.rect(20,20,300,300);
var img = new Image();
img.onload = function()
{
    rec.drawImage(img,30,30);
}
img.src = "monkey-face-cartoon.png";
rec.stroke();
function monkeyMove(){
}
</script>

It looks like the image isn't being loaded properly, and therefor img.onload isn't being called. 看起来图像未正确加载,因此未调用img.onload I swapped out the src for a data-uri and it rendered just fine. 我用src换了一个data-uri,它呈现的很好。 Where abouts are you getting the image from? 您从哪里获得图像?

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

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