gpt4 book ai didi

html - Canvas contenteditable 不工作

转载 作者:行者123 更新时间:2023-11-28 00:07:19 25 4
gpt4 key购买 nike

我将 Canvas 创建为 contenteditable ='true' 并将剪贴板中的图像粘贴到 Canvas 上。如下图所示,图像出现在 Canvas 旁边而不是 Canvas 顶部。尝试将 Canvas 转换为图像时,我得到空图像。这是因为我的 Canvas 没有保存我之前粘贴的图像。如何获取图像?

HTML

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
body{ background-color: ivory; }
canvas{border:1px solid red;}
</style>

<script>

function convert()
{
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
//ctx.fillRect(50,50,150,75);
var theImage=document.getElementById("toImage");
theImage.src=canvas.toDataURL();
}
</script>

</head>

<body>
<canvas id="canvas" width=300 height=300 contenteditable='true'></canvas>
<button type="button" onClick="convert()">Click</button>
<img id="toImage" width=300 height=300>
</body>
</html>

图片 enter image description here

最佳答案

contenteditable属性用于与文本相关的 HTML 元素,与 <canvas> 无关元素。

参见 MDN有关 contenteditable 的详细信息.

关于html - Canvas contenteditable 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17800500/

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