html - Canvas contenteditable 不工作

标签 html image canvas html5-canvas contenteditable

我将 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/

相关文章:

javascript - 无法编辑 HTML 文本输入中的最后两个字符

javascript - onMouseOver、超链接和 rel...帮助我 :(

java - 即使我在 actionPerformed -Java 中使用 ifs 设置了限制,图像也会移出框架

javascript - JavaScript 中的图像交换?

javascript - 如何让车子朝怪物方向行驶

javascript - 在 JavaScript 中使用鼠标事件在 Canvas 上绘制直线

javascript - 如何每 30 秒更改一次模态弹出窗口?

jquery - 图像按钮(左 + 右)jquery 使用 offset().left 进行右键单击,在 css 中使用 left 值进行左键单击只工作一次?

image - 通过 1 个人通过 ffmpeg 我需要组合 2 个音频并向其添加图片(专辑)

javascript - 使用 canvas.toDataURL 时如何设置 crossOrigin 属性?