javascript - 具有绝对位置的 html5 Canvas 不起作用

标签 javascript css html html5-canvas

具有绝对位置的 Canvas 不起作用,您可以在此处看到: http://jsfiddle.net/733zs/1/ (已在 Firefox 和 Chrome 中测试)矩形的大小应为 500x500 像素。

有没有办法让它在不手动设置宽度和高度的情况下工作?还是我做错了什么?

最佳答案

您必须手动设置宽度和高度。

这可以通过 JavaScript onresize 事件来完成 - 这很好,因为无论如何您几乎总是需要在调整大小时重绘 Canvas 表面。

编辑:

根据 W3 规范,这里是 Canvas DOM 界面:

http://www.w3.org/TR/html5/the-canvas-element.html

interface HTMLCanvasElement : HTMLElement {
           attribute unsigned long width;
           attribute unsigned long height;

  DOMString toDataURL(in optional DOMString type, in any... args);
  void toBlob(in FileCallback, in optional DOMString type, in any... args);

  object getContext(in DOMString contextId, in any... args);
};

The canvas element has two attributes to control the size of the coordinate space: width and height. These attributes, when specified, must have values that are valid non-negative integers. The rules for parsing non-negative integers must be used to obtain their numeric values. If an attribute is missing, or if parsing its value returns an error, then the default value must be used instead. The width attribute defaults to 300, and the height attribute defaults to 150.

关于javascript - 具有绝对位置的 html5 Canvas 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8358438/

相关文章:

javascript - 向 FontAwesome 图标添加事件监听器

jquery - 一旦它到达容器 div,将导航栏背景更改为黑色

css - Flexbox 在 Safari 中不堆叠

html - Margin 0 Auto block 适用于 50% 但不适用于 100%

javascript - 我可以将 Google Analytics 放在外部 JS 中吗?

JavaScript 创建对象的新实例

jquery - 在 Html 中获取 Paypal 捐赠交易详细信息并实现捐赠按钮

ios - HTML5 音频标签无法在 ios chrome 和 safari 中播放音频文件

jquery - 使用 Jquery 将整个 html 标签替换为另一个标签?

php - 同步JS+PHP时间