gpt4 book ai didi

javascript - 如何使用图像 DOM 元素作为 jCanvas 中源属性的值?

转载 作者:行者123 更新时间:2023-12-02 19:36:34 25 4
gpt4 key购买 nike

我想测试这个例子:http://calebevans.me/projects/jcanvas/sandbox.php#%24%28%22canvas%22%29.drawImage%28%7B%0A%20%20source%3A%20%24%28%27%23image-fish%27%29%5B0%5D%2C%0A%20%20x%3A%2050%2C%20y%3A%2050%2C%0A%20%20width%3A%20100%2C%0A%20%20fromCenter%3A%20false%0A%7D%29%3B

但除了默认的图像“img src”之外,我在浏览器中看不到任何结果。请有人帮助我。我该如何使用这个功能?

  <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<canvas width="500" height="250"></canvas>
</head>
<script src="jquery.js"></script>
<script src="jcanvas.js"></script>
<script>

$("canvas").drawImage({
source: $('#draggable')[0],
x: 50, y: 50,
width: 60,
fromCenter: false
});

</script>
<body>
<img src="../image/0.jpg" class="drag-image" id="draggable"/>
</div>
</body>
</html>

谢谢。

最佳答案

设置 Canvas 标签的源时,图像标签未完全加载。因此,$('#draggable')[0] 返回未定义。尝试在加载文档时调用drawImage。以下内容应该可以解决您的问题。

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<canvas width="500" height="250"></canvas>
</head>
<script src="jquery.js"></script>
<script src="jcanvas.js"></script>
<script>

$(document).ready(function(){
$("canvas").drawImage({
source: $('#draggable')[0],
x: 50, y: 50,
width: 60,
fromCenter: false
});
});

</script>
<body>
<img src="../image/0.jpg" class="drag-image" id="draggable"/>
</div>
</body>
</html>

关于javascript - 如何使用图像 DOM 元素作为 jCanvas 中源属性的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10866544/

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