国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > WEB開發(fā) > 正文

【Canvas】簡單的寫字板

2024-04-27 15:15:03
字體:
供稿:網(wǎng)友

這里寫圖片描述

演示地址:點(diǎn)我

<!DOCTYPE html><html><head> <meta charset="utf-8"> <script type="text/javascript" src="http://cdn.bootCSS.com/jquery/2.2.1/jquery.min.js"></script> <title>寫字板</title> <style type="text/css"> body,html { padding: 0; margin: 0; } a,div,span { font-family: "Arial","Microsoft YaHei","黑體","宋體",sans-serif; } .canvas-box { display: block; margin: 40px auto; background: #f5f5f5; } .color-box { width: 1080px; display: block; margin: 20px auto; text-align: center; } .color-item { display: inline-block; vertical-align: middle; width: 48px; height: 24px; margin: 10px; background: #989898; cursor: pointer; } .red { background: #e01d5b; } .blue { background: #1d6ae0; } .green { background: #1de087; } .yellow { background: #f3e41d; } .orange { background: #f9850b; } .black { background: #333; } .color-item.active { border: solid 3px #565656; } .btn { display: block; width: 120px; height: 40px; line-height: 40px; margin: 10px auto; text-align: center; font-size: 18px; border: solid 1px #999; border-radius: 5px; cursor: pointer; } </style></head><body> <canvas class="canvas-box" id="canvas"></canvas> <div class="color-box"> <span class="color-item red"></span> <span class="color-item blue"></span> <span class="color-item green"></span> <span class="color-item yellow"></span> <span class="color-item orange"></span> <span class="color-item black active"></span> </div> <div class="btn" onclick="clearDraw()">清除</div><script type="text/Javascript">var canvas = document.getElementById('canvas');var context = canvas.getContext("2d");var isDraw = false; //定義變量控制畫筆是否可用var movePos; //定義變量存放初始畫筆開始位置var linWidth = 10;var linColor = '#333';window.onload = function(){ draw();}function draw(){ canvas.width = 500; canvas.height = 500; context.strokeStyle = "red"; context.lineWidth = 10; context.beginPath(); context.strokeRect(0,0,500,500); //設(shè)置畫筆顏色,寬度 context.strokeStyle = "red"; context.lineWidth = 1; //使線段連續(xù),圓滑 context.lineCap = "round"; context.lineJoin = "round"; drawDashedLine(context,0,0,500,500); drawDashedLine(context,0,500,500,0); drawLine(context,0,250,500,250); drawLine(context,250,0,250,500);}//畫虛線(參照網(wǎng)上大神)function drawDashedLine(context, x1, y1, x2, y2, dashLength) { dashLength = dashLength === undefined ? 5 : dashLength; var deltaX = x2 - x1; var deltaY = y2 - y1; var numDashes = Math.floor( Math.sqrt(deltaX * deltaX + deltaY * deltaY) / dashLength); for (var i=0; i < numDashes; ++i) { context[ i % 2 === 0 ? 'moveTo' : 'lineTo' ] (x1 + (deltaX / numDashes) * i, y1 + (deltaY / numDashes) * i); } context.stroke();};//畫直線function drawLine(context,x1,y1,x2,y2){ context.moveTo(x1,y1); context.lineTo(x2,y2); context.stroke();};//獲取鼠標(biāo)相對與canvas位置function getPos(x,y){ var box = canvas.getBoundingClientRect(); return {x: x-box.left,y: y-box.top};};//畫筆function drawing(e){ if(isDraw){ var position = getPos(e.clientX,e.clientY); context.strokeStyle = linColor; context.lineWidth = linWidth; context.save(); context.beginPath(); context.moveTo(movePos.x,movePos.y); context.lineTo(position.x,position.y); context.stroke(); movePos = position; context.restore(); }}//鼠標(biāo)點(diǎn)下canvas.onmousedown = function(e){ isDraw = true; movePos = getPos(e.clientX,e.clientY); drawing(e);}//鼠標(biāo)移動canvas.onmousemove = function(e){ drawing(e);}//鼠標(biāo)松開canvas.onmouseup = function(e){ isDraw = false;}//鼠標(biāo)離開canvas.onmouSEOut =function(e){ isDraw = false;}//選擇畫筆顏色$('.color-item').on('click',function(){ $(this).addClass('active').siblings().removeClass('active'); linColor = $(this).css('background-color');});//清除function clearDraw(){ context.clearRect(0,0,500,500); draw();}</script></body></html>
上一篇:參透測試方法

下一篇:template string

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 玉山县| 土默特右旗| 泌阳县| 华池县| 英超| 年辖:市辖区| 天镇县| 台中县| 兴文县| 沈阳市| 监利县| 阿拉尔市| 荆州市| 大埔县| 阿拉尔市| 石屏县| 墨竹工卡县| 上栗县| 棋牌| 泾源县| 广西| 张掖市| 西畴县| 新田县| 南陵县| 丹凤县| 厦门市| 宁乡县| 天祝| 高安市| 宁化县| 安溪县| 郑州市| 高要市| 蕉岭县| 大兴区| 永安市| 新邵县| 澳门| 安国市| 信丰县|