Canvas HTML5 JavaScript代码不工作,用canvas.toDataURL() [英] Canvas HTML5 JavaScript code not working, with canvas.toDataURL()

查看:237
本文介绍了Canvas HTML5 JavaScript代码不工作,用canvas.toDataURL()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使此代码正常工作:

I've failed to get this code working:

(function() {
    // Creates a new canvas element and appends it as a child
    // to the parent element, and returns the reference to
    // the newly created canvas element


    function createCanvas(parent, width, height) {
        var canvas = {};
        canvas.node = document.createElement('canvas');
        canvas.context = canvas.node.getContext('2d');
        canvas.node.width = width || 100;
        canvas.node.height = height || 100;
        parent.appendChild(canvas.node);
        return canvas;
    }

    function init(container, width, height, fillColor) {
        var canvas = createCanvas(container, width, height);
        var ctx = canvas.context;
        // define a custom fillCircle method
        ctx.fillCircle = function(x, y, radius, fillColor) {
            this.fillStyle = fillColor;
            this.beginPath();
            this.moveTo(x, y);
            this.arc(x, y, radius, 0, Math.PI * 2, false);
            this.fill();
        };
        ctx.clearTo = function(fillColor) {
            ctx.fillStyle = fillColor;
            ctx.fillRect(0, 0, width, height);
        };
        ctx.clearTo(fillColor || "#ddd");

        // bind mouse events
        canvas.node.onmousemove = function(e) {
            if (!canvas.isDrawing) {
               return;
            }
            var x = e.pageX - this.offsetLeft;
            var y = e.pageY - this.offsetTop;
            var radius = 10; // or whatever
            var fillColor = '#ff0000';
            ctx.fillCircle(x, y, radius, fillColor);
        };
        canvas.node.onmousedown = function(e) {
            canvas.isDrawing = true;
        };
        canvas.node.onmouseup = function(e) {
            canvas.isDrawing = false;
        };
    }

    var container = document.getElementById('canvas');
    init(container, 200, 200, '#ddd');

})();

function hi(){
var canvas = document.getElementsByTagName('canvas');
var imageData = canvas.toDataURL();
    document.getElementById("his").innerHTML=imageData;
}

这是一个小的JavaScript代码,与画布 id 的<$ c> div 。

It's a little JavaScript code, which creates a little canvas in the div with the id of canvas.

并且,我试图使图像保存,并写入 div id of his 保存的图像。现在这是代码停止工作...我非常感谢您的帮助,谢谢! :)

And, I'm trying to make the image save, and write to a div with the id of his the saved image. NOW that's where the code stops working... I'd greatly appreciate your help, thanks! :)

推荐答案

document.getElementsByTagName('canvas') 返回 NodeList ,而不是单个元素。所以使用

document.getElementsByTagName('canvas') returns a NodeList, not a single element. So use

function hi(){
    var canvas = document.getElementsByTagName('canvas')[0];
    imageData = canvas ? canvas.toDataURL() : "could not find a <canvas> element";
    document.getElementById("his").textContent = imageData;
}

这篇关于Canvas HTML5 JavaScript代码不工作,用canvas.toDataURL()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆