<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo02 pc端米字格画布</title>
<link rel="stylesheet" href="css/canvas.css">
<script src="scripts/jquery-1.7.1.min.js"></script>
<style> </style>
</head>
<body>
<canvas id="canvas">您的浏览器不支持canvas</canvas>
<div id="controller"><div class="op_btn" id="clear_btn"> 清除</div></div>
<script src="scripts/canvas.js"></script>
</body>
</html>
 var canvasWidth = 800;
var canvasHeight =canvasWidth; var isMouseDown = false;
var lastLoc ;
// var lastLoc = {x:0,y:0};
var curTimestamp;
var lastTimestamp = 0;
var lineWidth; var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d'); canvas.width = canvasWidth;
canvas.height = canvasHeight;
drawGrid();
$('#clear_btn').click(function(){
context.clearRect(0,0,canvasWidth,canvasHeight);
drawGrid();
})
canvas.onmousedown = function(e){
e.preventDefault();
isMouseDown = true;
// console.log("mouse down!");
lastLoc = windowToCanvas(e.clientX,e.clientY);
lastTimestamp = new Date().getTime();
// alert(loc.x+","+loc.y); }
canvas.onmouseup = function(e){
e.preventDefault();
isMouseDown = false;
// console.log("mouse up~~~");
}
canvas.onmouseout = function(e){
e.preventDefault();
isMouseDown = false;
// console.log("mouse out~~");
} canvas.onmousemove = function(e){
e.preventDefault();
// isMouseDown = true;
if (isMouseDown) {
// console.log("mouse move");
var curLoc = windowToCanvas(e.clientX,e.clientY);
var s = calcDistance(curLoc , lastLoc);
var t = curTimestamp - lastTimestamp;
context.beginPath();
context.moveTo(lastLoc.x , lastLoc.y);
context.lineTo( curLoc.x , curLoc.y); context.strokeStyle = 'black';
context.lineWidth = 20;
context.lineCap="round"
context.lineJoin = "round" context.stroke(); lastLoc = curLoc;
curTimestamp = lastTimestamp;
lastLineWidth = lineWidth;
};
}
var maxLineWidth = 30;
var minLineWidth = 1 ;
var maxStrokeV = 10;
var minStrokeV = 0.1;
function calcLineWidth(t,s){
var v = s/t;
var resultLineWidth; if ( v <= minStrokeV)
resultLineWidth = maxLineWidth;
else if( v >= maxStrokeV)
resultLineWidth = minLineWidth;
else
resultLineWidth = maxLineWidth - (v-minStrokeV )/(maxStrokeV-minStrokeV)*(maxLineWidth-minLineWidth)
if (lastLineWidth == -1) {
return resultLineWidth;
}; return resultLineWidth*2/3 + resultLineWidth*1/3;
}
function calcDistance(loc1 , loc2){
return Math.sqrt((loc1.x - loc2.x)*(loc1.x - loc2.x) + (loc1.y - loc2.y)*(loc1.y - loc2.y));
}
function windowToCanvas(x,y){
var bbox = canvas.getBoundingClientRect();
return {x:Math.round(x-bbox.left),y:Math.round(y-bbox.top)};
} function drawGrid(){
context.save();
context.strokeStyle = "rgb(230,11,9)"; context.beginPath();
context.moveTo(3,3);
context.lineTo(canvasWidth - 3 , 3 );
context.lineTo(canvasWidth - 3 , canvasHeight - 3 );
context.lineTo(3 , canvasHeight - 3 );
context.closePath(); context.lineWidth = 6;
context.stroke(); context.beginPath();
context.moveTo(0,0);
context.lineTo(canvasWidth,canvasHeight); context.moveTo(canvasWidth,0);
context.lineTo(0,canvasHeight); context.moveTo(canvasWidth/2,0);
context.lineTo(canvasWidth/2,canvasHeight); context.moveTo(0,canvasHeight/2);
context.lineTo(canvasWidth,canvasHeight/2); context.lineWidth = 1;
context.stroke();
context.restore();
}

最新文章

  1. Xamarin.Android之布局文件智能提示问题
  2. Session中load/get方法的详细区别
  3. [POJ3111]K Best(分数规划, 二分)
  4. PHP读取Excel数据写入数据库(包含图片和文字)
  5. 快速了解SPA单页面应用
  6. java 多态奇怪现象,子类还没有构造完成就开始干活了,谁来帮我解释?
  7. Titanium系列--Titanium的简介、Titanium Studio安装和配置(一)
  8. 去除UITableView中多余的分割线或者隐藏cell间的分割线
  9. EF不同查询方法生成的SQL比较
  10. asp.net网站中添加百度地图功能
  11. [转]Kafka/Metaq设计思想学习笔记
  12. (转)linux中常用的头文件
  13. Quartz.NET作业调度框架详解
  14. Spring3.2新注解@ControllerAdvice
  15. 关闭数据库下的所有连接操作 sql存储过程
  16. 【javascript】ajax的参数
  17. scikit-learn的线性回归模型
  18. Linux 改变文件属性与权限
  19. 【LeetCode】Number of Islands
  20. cordova 跨平台APP版本升级

热门文章

  1. HDU1260 Tickets —— DP
  2. YTU 2586: 填空题B-字画鉴别
  3. javascript函数参数、返回值类型检查
  4. 关于追踪qemu 源码函数路径的一个方法
  5. eclipse下清除项目的svn信息
  6. Pascal之计算小系统
  7. asp.net mvc 学习资料
  8. 适用于PHP初学者的学习线路和建议
  9. spring分布式事务学习笔记(1)
  10. HDU-ACM“菜鸟先飞”冬训系列赛——第9场