<!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();
}

H5 canvas pc 端米字格 写字板的更多相关文章

  1. 社交系统/社群系统“ThinkSNS+”H5及PC端终于来了!一起来“找茬”

    [什么是TS+?] ThinkSNS(简称TS),一款全平台综合性社交系统,为国内外大中小企业和创业者提供社会化软件研发及技术解决方案,目前最新版本为ThinkSNS+,简称TS+. 还记得2017年 ...

  2. 关于H5从PC端切换到移动端,屏幕显示内容由横向转为竖向的研究!

    1.前言: 在项目中,我们常会遇见在手机端需要横屏观看的效果,而在pc端则默认切换到竖屏的样式. 或者是,UI提供的图是一个长图,但是在移动端我们需要让这个图在手机横屏时显示. 以上两个都需要我们实行 ...

  3. 实现pc端信纸留言板

    效果如图: 我好像在哪里见过这样的形式,但却从来没有想过怎么实现,有种莫名的兴奋感.怎么控制什么时候换行,怎么控制中间的线条,这些视乎都是CSS无法实现的,我陷入了死局.寻找JS的做法,JS的挺复杂的 ...

  4. canvas做的一个写字板

    <!DOCTYPE html><html><head><title>画板实验</title> <meta charset=" ...

  5. canvas h5制作写字板

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <script type ...

  6. H5测试与PC端测试不同的点

    1.通过H5网页(非手机的返回功能)的返回功能可以返回,不会出现无法返回的情况. 2.横屏竖屏相互切换,能自适应,并且布局不会乱掉: 3.为能在不同分辨率的手机上能更好的展示,建议采用响应式设计(如: ...

  7. h5微信页面在手机微信端和微信web开发者工具中都能正常显示,但是在pc端微信浏览器上打不开(显示空白)

    h5微信页面在手机微信和微信开发者工具中都能正常显示,但是在pc端微信浏览器上打不开或者数据加载不出来. 原因:pc端微信浏览器不支持ES6语法,我的代码中使用了一些ES6的特性 解决:将ES6转换为 ...

  8. html5之canvas画图 1.写字板功能

     写字板事例:       写字板分析:1.点击鼠标開始写字(onmosedown)2.按下鼠标写字(onmousemove)3.松开鼠标,停下写字(撤销onmousemove事件):       代 ...

  9. 前端:微信支付和支付宝支付在pc端和h5页面中的应用

    1:h5微信支付 使用的是https://pay.weixin.qq.com/wiki/doc/api/index.html  中的 (1):公司需要首先要配置公众号微信支付地址和测试白名单(支付的时 ...

随机推荐

  1. UVA11419 SAM I AM —— 最小点覆盖 + 输出覆盖点集

    题目链接:https://vjudge.net/problem/UVA-11419 题解: 1.二分图匹配之最小点覆盖.:把x坐标和y坐标看成是点, 图中的目标看成是边,所以最终的目的是求出用最少的点 ...

  2. Module in powershell

    https://docs.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershell-6 ht ...

  3. 《Visual C++ 2010入门教程》系列四:VC2010中初学者常见错误、警告和问题

    <Visual C++ 2010入门教程>系列四:VC2010中初学者常见错误.警告和问题   这一章将帮助大家解释一些常见的错误.警告和问题,帮助大家去理解和解决一些常见问题,并了解它的 ...

  4. bzoj 5281 Talent Show —— 01分数规划+背包

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5281 二分一个答案比值,因为最后要*1000,不如先把 v[] *1000,就可以二分整数: ...

  5. 从0开始学习Hadoop(2)安装JDK

    参考文档: 安装包方式安装:http://www.cnblogs.com/wuyudong/p/ubuntu-jdk8.html PPA方式安装:推荐  http://www.cnblogs.com/ ...

  6. EF 6.0 The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. c#中的时间

    在SQL server 中,有两种日期时间类型: 1.DateTime 它的范围是 1753-1-1 至 9999-12-31 2.DateTime2 它的满园是 0001-01-01 至 9999- ...

  7. wireshark笔记(1)之工具认识

    1 下载链接  https://www.wireshark.org/ 安装只需要注意同时会安装winpcap就好了 相关链接:www.wiresharkbook.com //书籍 英文 www.wik ...

  8. arcgis for silverlight 鼠标点击地图获取当前经纬度

    silverlight代码: 地图的name值是myMap.后台在页面初始化的时候,添加地图的点击事件 myMap.MouseClick+=new EventHandler<Map.MouseE ...

  9. python之对堆栈、队列处理操作(转载+个人看法)

    参考链接:https://blog.csdn.net/u010786109/article/details/40649827 python实现堆栈操作 堆栈是一个后进先出的数据结构,其工作方式就像一堆 ...

  10. poj 3164 Command Network (朱刘算法)

    题目链接: http://poj.org/problem?id=3164 题目大意: 有n个点(用坐标表示)各点编号分别为1—>n,m条单向路,问能否存在一个花费价值最小的网络,能使从1点到达任 ...