H5 canvas pc 端米字格 写字板
- <!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 端米字格 写字板的更多相关文章
- 社交系统/社群系统“ThinkSNS+”H5及PC端终于来了!一起来“找茬”
[什么是TS+?] ThinkSNS(简称TS),一款全平台综合性社交系统,为国内外大中小企业和创业者提供社会化软件研发及技术解决方案,目前最新版本为ThinkSNS+,简称TS+. 还记得2017年 ...
- 关于H5从PC端切换到移动端,屏幕显示内容由横向转为竖向的研究!
1.前言: 在项目中,我们常会遇见在手机端需要横屏观看的效果,而在pc端则默认切换到竖屏的样式. 或者是,UI提供的图是一个长图,但是在移动端我们需要让这个图在手机横屏时显示. 以上两个都需要我们实行 ...
- 实现pc端信纸留言板
效果如图: 我好像在哪里见过这样的形式,但却从来没有想过怎么实现,有种莫名的兴奋感.怎么控制什么时候换行,怎么控制中间的线条,这些视乎都是CSS无法实现的,我陷入了死局.寻找JS的做法,JS的挺复杂的 ...
- canvas做的一个写字板
<!DOCTYPE html><html><head><title>画板实验</title> <meta charset=" ...
- canvas h5制作写字板
<!DOCTYPE html><html><head> <meta charset="utf-8"> <script type ...
- H5测试与PC端测试不同的点
1.通过H5网页(非手机的返回功能)的返回功能可以返回,不会出现无法返回的情况. 2.横屏竖屏相互切换,能自适应,并且布局不会乱掉: 3.为能在不同分辨率的手机上能更好的展示,建议采用响应式设计(如: ...
- h5微信页面在手机微信端和微信web开发者工具中都能正常显示,但是在pc端微信浏览器上打不开(显示空白)
h5微信页面在手机微信和微信开发者工具中都能正常显示,但是在pc端微信浏览器上打不开或者数据加载不出来. 原因:pc端微信浏览器不支持ES6语法,我的代码中使用了一些ES6的特性 解决:将ES6转换为 ...
- html5之canvas画图 1.写字板功能
写字板事例: 写字板分析:1.点击鼠标開始写字(onmosedown)2.按下鼠标写字(onmousemove)3.松开鼠标,停下写字(撤销onmousemove事件): 代 ...
- 前端:微信支付和支付宝支付在pc端和h5页面中的应用
1:h5微信支付 使用的是https://pay.weixin.qq.com/wiki/doc/api/index.html 中的 (1):公司需要首先要配置公众号微信支付地址和测试白名单(支付的时 ...
随机推荐
- java抛出异常后,后续代码是否可继续执行
参考:https://www.cnblogs.com/wangyingli/p/5912269.html 仅此可正常执行异常后内容 try{ throw new Exception("参数越 ...
- 微信公众号开发——获取access_token(PHP版)
access_token是调用微信接口的唯一凭据,每两小时刷新一次,我们需要每两小时就获取一次access_token. <?php class TokenUtil { //获取access_t ...
- poj1419 求最大独立集
题目链接:http://poj.org/problem?id=1419 题意:求最大独立集 思路: 这里有一个定理: 最大独立集=补图的最大团最大团=补图的最大独立集 所以这里我们只要求给出的图的最大 ...
- SSM整合(一)
http://www.cnblogs.com/xuerong/p/6796600.html 技术点 1.基础框架-ssm (SpringMVC +Spring +MyBatis) 2.数据库MySqQ ...
- 在Visual studio 中解除 TFS 的账号绑定
在Visual Studio中, 只要使用了TFS, 就会要求输入用户名密码验证 . 但是一旦点击验证对话框下部的:记住用户名密码 以后都不能再修改用户名了. 而且重装Visual Studio 听说 ...
- 洛谷 P3952 时间复杂度【模拟】
把No写成NO,WA了一发-- 现在看这题也不难-- 用一个栈,记一下前面F的字母,是否合法,合法的有多长,每次入栈弹栈即可 #include<iostream> #include< ...
- bzoj 3052: [wc2013]糖果公园【树上带修改莫队】
参考:http://blog.csdn.net/lych_cys/article/details/50845832 把树变成dfs括号序的形式,注意这个是不包含lca的(除非lca是两点中的一个) 然 ...
- 今天带来Pycharm安装详细教程
Python环境搭建—安利Python小白的Python和Pycharm安装详细教程 人生苦短,我用Python.众所周知,Python目前越来越火,学习Python的小伙伴也越来越多.最近看到群里的 ...
- Luogu P1134 阶乘问题 【数学/乱搞】 By cellur925
输入输出格式 输入格式: 仅一行包含一个正整数 NN . 输出格式: 一个整数,表示最右边的非零位的值. 输入输出样例 输入样例#1: 12 输出样例#1: 6 说明 USACO Training S ...
- python中threading模块中最重要的Tread类
Tread是threading模块中的重要类之一,可以使用它来创造线程.其具体使用方法是创建一个threading.Tread对象,在它的初始化函数中将需要调用的对象作为初始化参数传入. 具体代码如下 ...