gpt4 book ai didi

javascript - 使用 Javascript 在 Canvas 中显示图像?

转载 作者:太空狗 更新时间:2023-10-29 14:05:04 25 4
gpt4 key购买 nike

我希望能够单击我页面上的按钮并将图像加载到某个 X、Y 坐标的 Canvas 中?

下面的代码是我下面的。我希望图像位于 image/photo.jpg 或同一目录中,但最好位于主页的子目录中。

**问题:如何通过点击网页上的按钮使 JPG 显示在 Canvas 中?

代码:

<!DOCTYPE html>
<html>

<script>
function draw(){
var ctx = document.getElementById("myCanvas").getContext("2d");
var img = new Image():
// img.src = "2c.jpg";
img.src = "/images/2c.jpg";

ctx.drawImage(img,0,0);
}


</script>


<body background="Black">

<div align="center">
<button type="button" onclick="draw()">Show Image on Canvas</button>

<canvas id="myCanvas" width="900" height="400" style="border:2px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.
</canvas>
</div>

<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.font="20px Arial";

ctx.fillText("Royal Flush $",500,50);
ctx.fillText("Striaght Flush $",500,80);
ctx.fillText("Flush $",500,110);
ctx.fillText("Four of a Kind $",500,140);
ctx.fillText("Full House $",500,170);
ctx.fillText("Three of a Kind $",500,200);
ctx.fillText("Two Pair $",500,230);
ctx.fillText("Pair of ACES $",500,260);


ctx.rect(495,10,270,350);
ctx.stroke();
</script>

</body>
</html>

2014 年 3 月 6 日代码:

下面的代码为什么不起作用。你必须在 Canvas 上有 ID 标签吗?该页面将显示,但由于某种原因,单击按钮时图像不会显示。该图像与我的 index.html 文件位于同一目录中。

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>

<style type="text/css">
canvas{
border: 5px solid black;
}
</style>

</html>
<button id="galaxy">Add image #1</button>
<button id="circles">Add image #2</button><span></span>
<canvas width="500" height="500"></canvas>

<script>
var Images = {};

function loadImages(list){
var total = 0;
document.querySelector("span").innerText = "...Loading...";
for(var i = 0; i < list.length; i++){
var img = new Image();
Images[list[i].name] = img;
img.onload = function(){
total++;
if(total == list.length){
document.querySelector("span").innerText = "...Loaded.";
}
};
img.src = list[i].url;
}
}

function drawImage(img){
var ctx = document.querySelector("canvas").getContext("2d");
ctx.drawImage(Images[img], 0, 0, 50, 50);
}

loadImages([{
name: "2c.jpg",
url: "mp.jpg"
},{
name: "mp.jpg",
url: "mp.jpg"
}]);

document.querySelector("#galaxy").addEventListener("click", function(){
drawImage("galaxy");
});

document.querySelector("#circles").addEventListener("click", function(){
drawImage("weirdCircles");
});

</script>

</html>

最佳答案

等到图像加载后再绘制:

var img = new Image();
img.onload = function(){ /*or*/ img.addEventListener("load", function(){
ctx.drawImage(img,0,0); ctx.drawImage(img,0,0);
}; };
img.src = "/images/2c.jpg";

演示:http://jsfiddle.net/DerekL/YcLgw/


如果您的游戏中有多个图像,

最好在开始之前预加载所有图像。

预加载图片:http://jsfiddle.net/DerekL/uCQAH/ (没有 jQuery:http://jsfiddle.net/DerekL/Lr9Gb/)


如果你更熟悉OOP : http://jsfiddle.net/DerekL/2F2gu/

function ImageCollection(list, callback){
var total = 0, images = {}; //private :)
for(var i = 0; i < list.length; i++){
var img = new Image();
images[list[i].name] = img;
img.onload = function(){
total++;
if(total == list.length){
callback && callback();
}
};
img.src = list[i].url;
}
this.get = function(name){
return images[name] || (function(){throw "Not exist"})();
};
}

//Create an ImageCollection to load and store my images
var images = new ImageCollection([{
name: "MyImage", url: "//example.com/example.jpg"
}]);

//To pick and draw an image from the collection:
var ctx = document.querySelector("canvas").getContext("2d");
ctx.drawImage(images.get("MyImage"), 0, 0);

关于javascript - 使用 Javascript 在 Canvas 中显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22213555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com