HTML5动态创建画布 [英] HTML5 Dynamically create Canvas

查看:903
本文介绍了HTML5动态创建画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个关于使用javascript动态创建画布的问题。

Hi there i have a question about dynamically creating a canvas using javascript.

我创建了一个这样的画布:

i create a canvas like this:

var canvas = document.createElement('canvas');
canvas.id     = "CursorLayer";
canvas.width  = 1224;
canvas.height = 768;
canvas.style.zIndex   = 8;
canvas.style.position = "absolute";
canvas.style.border   = "1px solid";

但是当我尝试找到它时,我得到一个 null value:

but when I try to locate it, I get a null value:

cursorLayer = document.getElementById("CursorLayer");

我做错了吗?是否有更好的方法使用JavaScript创建画布?

Am I doing it wrong? Is there a better way to create a canvas using JavaScript?

推荐答案

问题是,您不要将canvas元素

The problem is that you do not insert your canvas element in the document body.

只需执行以下操作:

document.body.appendChild(canvas);

DEMO: http://jsfiddle.net/8DEsJ/

这篇关于HTML5动态创建画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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