转:Canvas标签的width和height以及style.width和style.height的区别
转自:http://www.cnblogs.com/artwl/archive/2012/02/28/2372042.html 作者:Artwl
背景
今天在博问中看到一个问题:用canvas 的 lineto方法画对角线,但画出来的图形不对?
这是一个很常见的误区,这里给大家细说一下。
原理
在w3网站上是这样解释的:
The
canvaselement has two attributes to control the size of the coordinate space:widthandheight. 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. Thewidthattribute defaults to 300, and theheightattribute defaults to 150.The intrinsic dimensions of the
canvaselement equal the size of the coordinate space, with the numbers interpreted in CSS pixels. However, the element can be sized arbitrarily by a style sheet. During rendering, the image is scaled to fit this layout size.
其实这里已经说的很明白,通俗点说就是在canvas中定义width、height跟在style中定义width和height是不同的,canvas标签的width和height是画布实际宽度和高度,绘制的图形都是在这个上面。而style的width和height是canvas在浏览器中被渲染的高度和宽度。如果canvas的width和height没指定或值不正确,就被设置成默认值(width:300px,height:150px)。
我们可以利用style的width和height来缩放canvas,请看下面的示例。
示例
示例代码如下:
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function drawDiagonal(id){
var canvas=document.getElementById(id);
var context=canvas.getContext("2d");
context.beginPath();
context.moveTo(0,0);
context.lineTo(100,100);
context.stroke();
} window.onload=function(){
drawDiagonal("diagonal1");
drawDiagonal("diagonal2");
drawDiagonal("diagonal3");
}
</script>
</head>
<body>
<canvas id="diagonal1" style="border:1px solid;" width="100px" height="100px"></canvas>
<canvas id="diagonal2" style="border:1px solid;width:200px;height:200px;" width="100px" height="100px"></canvas>
<canvas id="diagonal3" style="border:1px solid;width:200px;height:200px;"></canvas>
</body>
</html>
转:Canvas标签的width和height以及style.width和style.height的区别的更多相关文章
- 在canvas标签和style中定义width和height
在canvas标签中定义width.height跟在style中定义width.height是不同的.canvas标签的width和height是画布实际宽度和高度,就是在这个上面绘制图形.style ...
- canvas基础入门(一)canvas的width、height于css样式中的宽高区别
canvas的width.height于css样式中的宽高对画布的内容显示是有所区别的 1.在canvas标签下调用他的width和height,而且是没有单位的宽高,这种指定canvas大小的方法也 ...
- canvas标签(1)--线条、矩形、圆形、文本、阴影、抛小球
从网上扒拉的代码,敲了敲代码玩. html页面显示内容很简单,只要写个canvas标签,给他一个id写js就可以了 <!DOCTYPE html> <html> <hea ...
- html5 canvas标签
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- HTML5新增Canvas标签及对应属性、API详解(基础一)
知识说明: HTML5新增的canvas标签,通过创建画布,在画布上创建任何想要的形状,下面将canvas的API以及属性做一个整理,并且附上时钟的示例,便于后期复习学习!Fighting! 一.标签 ...
- html5之canvas困惑 在canvas标签内需要设置了宽跟高,如果在css中设置同样的宽跟高,画出来的图像变形了?
<canvas class="cvs"></canvas> 遇到的问题: 如css 中设.cvs{width:500px;height:400px;},也就 ...
- [html5] 学习笔记-Canvas标签的使用
Canvas通过JavaScript来绘制2D图形.Canvas是逐像素渲染的.在Canvas中,一旦图形被绘制完成,它就不会继续得到浏览器的关注.如果其位置发生变化,那么整个场景也需要重新绘制,包括 ...
- canvas标签的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HTML5的canvas标签制作黑客帝国里的简单画面
1.加入canvas标签 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> ...
随机推荐
- MySQL会创建临时表的几种情况
1.UNION查询: 2.用到TEMPTABLE算法或者是UNION查询中的视图: 3.ORDER BY和GROUP BY的子句不一样时: 4.表连接中,ORDER BY的列不是驱动表中的:(指定了联 ...
- nodejs+gulpjs压缩js代码
1.安装node.js 下载地址:nodejs.org 或者 nodejs.cn 2.安装gulp之前我们需要安装nodejs的环境,检测能够正常使用npm后,我们用npm对gulp进行一次全局安 ...
- Intellij IDEA 使用spring-boot-devtools无效解决办法一
Intellij IDEA 使用spring-boot-devtools maven依赖 ``` <dependency> <groupId>org.springframewo ...
- hihoCoder 1513 小Hi的烦恼
hihoCoder 1513 小Hi的烦恼 思路: 用bitset判断交集个数 代码: #include<bits/stdc++.h> using namespace std; #defi ...
- Java checked 异常 和 RuntimeException(运行时异常)
目录 一.运行时异常 1.什么是RuntimeExceptioin 2.运行时异常的特点 3.如何运用运行时异常 二.运行时异常和ckecked异常的区别 1.机制上 2.逻辑上 一.运行时异常 1. ...
- Confluence 6 导入 Active Directory 服务器证书 - UNIX
为了让你的应用服务器能够信任你的目录服务器.你目录服务器上导出的证书需要导入到你应用服务器的 Java 运行环境中.JDK 存储了信任的证书,这个存储信任证书的文件称为一个 keystore.默认的 ...
- JVM自定义类加载器加载指定classPath下的所有class及jar
一.JVM中的类加载器类型 从Java虚拟机的角度讲,只有两种不同的类加载器:启动类加载器和其他类加载器. 1.启动类加载器(Boostrap ClassLoader):这个是由c++实现的,主要负责 ...
- thinkphp得到客户端的ip
/** * 获取客户端IP地址 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 * @return mixed */function get_cli ...
- Oracle EBS供应商接口导入(转)
原文地址 Oracle EBS供应商接口导入 1.供应商导入组成供应商导入主要分为供应商头信息导入.供应商地点信息导入.供应商联系人导入三个部分的信息,其他按实际需求进行添加.供应商头信息导入:导入供 ...
- en_e outtest2
e 1◆ e i: ə ɜː e I 2◆ ei ey ei i: 3◆ eer ɪə 4◆ ee i: 5◆ er ə 6◆ ere ɪə eə 7◆ ea ɪə i: ə ...