javascript - JavaScript html5 Canvas

标签 javascript html canvas

单击按钮时我只想将元素放入 Canvas 中,但它不起作用。这是我编写的代码:

<script language="javascript" type="text/javascript">
function image()
{
    var c=document.getElementById("myCanvas");   
    var ctx=c.getContext("2d");
    ctx.clear();
    ctx.setTransform(-1, 0, 0, 1, 200, 200);
    context.clear(true)
    var imgobj=new Image();
    imgobj.onload=function()
    {
        ctx.drawImage(imgobj,69,50);
    }
    imgobj.src='images.jpg';
}

function circle()
{
    var c=document.getElementById("myCanvas");
    var ctx=c.getContext("2d");
    ctx.clear();

    // do some more drawing
    ctx.setTransform(-1, 0, 0, 1, 200, 200);
    // do some drawing with the new transform
    context.clear(true);
    ctx.beginPath();
    ctx.arc(95,50,40,0,2*Math.PI);
    ctx.stroke();
}

function line()
{
    var c=document.getElementById("myCanvas");
    var ctx=c.getContext("2d");
    ctx.clear();

    // do some more drawing
    ctx.setTransform(-1, 0, 0, 1, 200, 200);
    // do some drawing with the new transform
    context.clear(true);
    ctx.moveTo(0,0);
    ctx.lineTo(200,100);
    ctx.stroke();

</script> 

</head>   
<body>
    <h1>Demo of HTM5</h1>
    <div class="canvas1" align="left">
        <canvas id="myCanvas" width="300" height="300" style="border:1px solid #000000;">
        </canvas>
        <p><button  onclick="circle()"> Draw Circle</button></p>
        <p><button onclick="line()"> Draw Line</button></p>
        <p><button onclick="image()"> Display Image</button></p>
    </div>
</body>
</html>

最佳答案

上下文对象上没有 ctx.clear() 方法,请尝试使用:

ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);

您还使用了未分配的上下文,应将其删除。

此外,无需每次在函数内查找 Canvas ,只需在全局范围内查找一次即可。

而且,在您的 line 方法中,您缺少 beginPath() 调用。

<强> See fiddle here

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

function image() {

    /// use this to clear canvas
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);

    ctx.setTransform(-1, 0, 0, 1, 200, 200);
    var imgobj = new Image();
    imgobj.onload = function () {
        ctx.drawImage(imgobj, 69, 50);
    }
    imgobj.src = 'images.jpg';
}

function circle() {
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);

    // do some more drawing
    ctx.setTransform(-1, 0, 0, 1, 200, 200);
    // do some drawing with the new transform
    ctx.beginPath();
    ctx.arc(95, 50, 40, 0, 2 * Math.PI);
    ctx.stroke();
}

function line() {
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);

    // do some more drawing
    ctx.setTransform(-1, 0, 0, 1, 200, 200);
    // do some drawing with the new transform

    ctx.beginPath();  /// add a beginPath here
    ctx.moveTo(0, 0);
    ctx.lineTo(200, 100);
    ctx.stroke();
}

关于javascript - JavaScript html5 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21422504/

相关文章:

javascript - 根据 mySQL 数据库中的数据动态更改 svg 图像元素

javascript - 使用 superagent-cache 缓存不同参数的 HTTP 响应

html - 为什么我在这里看不到谷歌的应用程序图标?

javascript - 如何按比例调整 Canvas 中的图像大小?

javascript - Canvas fillRect 高度和宽度总是倾斜/扭曲

Javascript - 修改 HTML 标签上的 CSS?

javascript - Sonarqube 不从 LCOV 检索我的 JavaScript 覆盖率

php - <div> 类没有在 php 中显示

javascript - 如何以编程方式添加下标(html <sub> 标签)?

javascript - 如何从整个图像的缓存像素数据中获取 Canvas 图像数据的矩形部分?