JavaScript 画布对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16180924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:36:34  来源:igfitidea点击:

JavaScript Canvas object

javascriptcanvas

提问by Get Off My Lawn

I want to create a canvas object in memory, and not require a HTML <canvas>tag. Is this possible?

我想在内存中创建一个画布对象,并且不需要 HTML<canvas>标签。这可能吗?

With this code:

使用此代码:

var canvas = new Canvas();
var ctx = canvas.getContext('2d');

I get this error message: Uncaught ReferenceError: Canvas is not defined

我收到此错误消息: Uncaught ReferenceError: Canvas is not defined

回答by ryanbrill

You should be able to create the element with JavaScript:

您应该能够使用 JavaScript 创建元素:

var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');

回答by Bergi

Use document.createElement("canvas")instead. There is no Canvasconstructor for canvases, as you know it from Imagefor images or Optionfor options - those are the sole exceptions.

使用document.createElement("canvas")来代替。画布没有Canvas构造函数,正如您从图像或选项中知道的那样- 这些是唯一的例外。ImageOption