<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#myCanvas{
background-color: #fde;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="1000" height="500"></canvas>
</body>
<script type="text/javascript">
var oc = document.getElementById("myCanvas"); var ctx = oc.getContext("2d"); var isAdd ;
var obj = {
x : 50,
y: 50,
width : 200,
height : 200
}
var num = 0
function rotate(){
ctx.save();
if(num==200){
isAdd = false;
}
if(num==0){
isAdd = true;
}
if(isAdd){
num++;
}
else{
num--;
}
ctx.clearRect(0,0,500,500);
ctx.fillStyle="blueviolet";
ctx.translate(obj.x+(obj.width)/2,obj.y+(obj.height)/2);
ctx.scale(num/100,num/100);
ctx.rotate(num*Math.PI/180); ctx.translate(-(obj.x+(obj.width)/2),-(obj.y+(obj.height)/2))
ctx.fillRect(obj.x,obj.y,obj.width,obj.height);
ctx.restore();
}
setInterval(function(){
rotate(); },10); </script>
</html>

在Canvas中添加save()和restore()方法是因为Canvas的绘制实在上一个绘制图形之后继续绘制的(覆盖)。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
body{
background: red;
}
#canvas{
background: white;
width: 400px;
height: 400px;
} </style>
</head>
<body>
<!-- 默认canvas大小width300px,height150px -->
<!-- 宽高必须在canvas标签的属性中设置,在css中写的话,是将canvas进行拉伸 -->
<canvas id="canvas" width="400" height="400">
<span>这是一个画布,请用ie10+浏览器查看,或者。。。。</span>
</canvas> <input type="color" id="colorInput"/>
<input type="number" id="numberInput" value="1"/> <script>
//得到画布
var oC = document.getElementById('canvas');
//得到canvas的上下文环境
//目前只支持2d环境
var oGC = oC.getContext('2d'); oC.onmousedown = function(ev){
var colorInput = document.getElementById('colorInput');
var numberInput = document.getElementById('numberInput');
oGC.strokeStyle = colorInput.value;
oGC.lineWidth = numberInput.value;
oGC.beginPath(); var ev = ev || window.event;
//得到按下这个点相对于canvas的位置
oGC.moveTo(ev.clientX - oC.offsetLeft, ev.clientY - oC.offsetTop); document.onmousemove = function(ev){
var ev = ev || window.event;
oGC.lineTo(ev.clientX - oC.offsetLeft, ev.clientY - oC.offsetTop);
oGC.stroke();
} document.onmouseup = function(){
oGC.closePath();
document.onmousemove = null;
document.onmouseup = null;
} } </script>
</body>
</html>

demo_06Canvas的更多相关文章

随机推荐

  1. google搜索新姿势

    大前提:英文Google→http://www.google.com或http://www.google.cn 第一篇 在搜索框上输入:"indexof/"inurl:lib 再按 ...

  2. error C2589: “(”: “::”右边的非法标记 error C2059: 语法错误 : “::

    1. 错误输出 ./zlibrary/ui/src/win32/w32widgets/W32VBorderBox.cpp(114) : error C2589: “(”: “::”右边的非法标记    ...

  3. psd via fft and pwelch

    %fft and pwelch方法求取功率谱load x.mat Fs = 1; t = (0:1/Fs:1-1/Fs).'; Nx = length(x); % Window data w = ha ...

  4. HTML几类标签的应用总结

    打开DREAMWEAVER,新建HTML,如下图: body的属性: bgcolor 页面背景色 background  背景壁纸.图片 text  文字颜色 topmargin  上边距 leftm ...

  5. ThinkPHP3.1新特性:Action参数绑定

    Action参数绑定功能提供了URL变量和操作方法的参数绑定支持,这一功能可以使得你的操作方法定义和参数获取更加清晰,也便于跨模块调用操作方法了.这一新特性对以往的操作方法使用没有任何影响,你也可以用 ...

  6. android94 样式和主题

    style.xml <resources xmlns:android="http://schemas.android.com/apk/res/android"> < ...

  7. linux shell read command-Getting User Input Via Keyboard--ref

    ref:http://bash.cyberciti.biz/guide/Getting_User_Input_Via_Keyboard You can accept input from the ke ...

  8. Amazon EC2上搭建VPN服务器

    Amazon EC2 提供了一年免费试用,Micro Instance,配置是 1G 内存,共享 CPU,和每月 15G 的流量.搭一个 VPN 服务器绰绰有余了.操作系统我选的是 Amazon Li ...

  9. javascript 中的new操作符的理解

    new 操作符 在有上面的基础概念的介绍之后,在加上new操作符,我们就能完成传统面向对象的class + new的方式创建对象,在Javascript中,我们将这类方式成为Pseudoclassic ...

  10. dom+bom

    一.判断最大值和最小值,注:arr为数组 最大值:Math.max.apply(null, arr); 最小值:Math.min.apply(null, arr);   二.BOM 打开新页面和关闭打 ...