JS+HTML5 Canvas實(shí)現(xiàn)簡(jiǎn)單的寫(xiě)字板功能示例
本文實(shí)例講述了JS+HTML5 Canvas實(shí)現(xiàn)簡(jiǎn)單的寫(xiě)字板功能。分享給大家供大家參考,具體如下:
先來(lái)看運(yùn)行效果:

具體代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>www.dbjr.com.cn JS寫(xiě)字板</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<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; //定義變量控制畫(huà)筆是否可用
var movePos; //定義變量存放初始畫(huà)筆開(kāi)始位置
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è)置畫(huà)筆顏色,寬度
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);
}
//畫(huà)虛線(參照網(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();
};
//畫(huà)直線
function drawLine(context,x1,y1,x2,y2){
context.moveTo(x1,y1);
context.lineTo(x2,y2);
context.stroke();
};
//獲取鼠標(biāo)相對(duì)與canvas位置
function getPos(x,y){
var box = canvas.getBoundingClientRect();
return {x: x-box.left,y: y-box.top};
};
//畫(huà)筆
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)移動(dòng)
canvas.onmousemove = function(e){
drawing(e);
}
//鼠標(biāo)松開(kāi)
canvas.onmouseup = function(e){
isDraw = false;
}
//鼠標(biāo)離開(kāi)
canvas.onmouseout =function(e){
isDraw = false;
}
//選擇畫(huà)筆顏色
$('.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>
感興趣的朋友還可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼,親身體驗(yàn)一下運(yùn)行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容還可查看本站專(zhuān)題:《JavaScript+HTML5特效與技巧匯總》、《JavaScript圖形繪制技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- 在小程序Canvas中使用measureText的方法示例
- js canvas實(shí)現(xiàn)寫(xiě)字動(dòng)畫(huà)效果
- js+html5實(shí)現(xiàn)canvas繪制鏤空字體文本的方法
- Javascript保存網(wǎng)頁(yè)為圖片借助于html2canvas庫(kù)實(shí)現(xiàn)
- javascript結(jié)合html5 canvas實(shí)現(xiàn)(可調(diào)畫(huà)筆顏色/粗細(xì)/橡皮)的涂鴉板
- JavaScript+html5 canvas實(shí)現(xiàn)本地截圖教程
- html5 canvas js(數(shù)字時(shí)鐘)實(shí)例代碼
- JS+Canvas 實(shí)現(xiàn)下雨下雪效果
- js使用html2canvas實(shí)現(xiàn)屏幕截取的示例代碼
- JS使用canvas中的measureText方法測(cè)量字體寬度示例
相關(guān)文章
史上最全JavaScript常用的簡(jiǎn)寫(xiě)技巧(推薦)
這篇文章主要介紹了JavaScript常用的簡(jiǎn)寫(xiě)技巧,列舉了20條js中常用的簡(jiǎn)寫(xiě)技巧,具體操作步驟大家可查看下文的詳細(xì)講解,感興趣的小伙伴們可以參考一下2017-08-08
一個(gè)極為簡(jiǎn)單的requirejs實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇一個(gè)極為簡(jiǎn)單的requirejs實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
baidu博客的編輯友情鏈接的新的層窗口!經(jīng)典~支持【FF】
baidu博客的編輯友情鏈接的新的層窗口!經(jīng)典~支持【FF】...2007-02-02
利用JavaScript實(shí)現(xiàn)一個(gè)日期范圍選擇器
日期范圍選擇器是一個(gè)常見(jiàn)的Web應(yīng)用功能,它允許用戶選擇一個(gè)日期范圍,本文我們將使用JavaScript來(lái)實(shí)現(xiàn)這個(gè)功能,感興趣的小伙伴可以了解下2024-01-01
實(shí)現(xiàn)JavaScript高性能的數(shù)據(jù)存儲(chǔ)
本文主要對(duì)JavaScript的數(shù)據(jù)存儲(chǔ),產(chǎn)生性能問(wèn)題的原因,內(nèi)存泄露的幾種情況等做了簡(jiǎn)要分析介紹,需要的朋友可以看下2016-12-12
符合W3C網(wǎng)頁(yè)標(biāo)準(zhǔn)的iframe標(biāo)簽的使用方法
符合W3C網(wǎng)頁(yè)標(biāo)準(zhǔn)的iframe標(biāo)簽的使用方法...2007-07-07
uni-app 自定義底部導(dǎo)航欄的實(shí)現(xiàn)
這篇文章主要介紹了uni-app 自定義底部導(dǎo)航欄的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
javascript實(shí)現(xiàn)tabs選項(xiàng)卡切換效果(擴(kuò)展版)
常用的頁(yè)面效果有彈出層效果,無(wú)縫滾動(dòng)效果,選項(xiàng)卡切換效果,接下來(lái)與大家分享一款自己用原生javascript寫(xiě)的選項(xiàng)卡切換效果在原有的基礎(chǔ)上進(jìn)行了擴(kuò)展,加入了自動(dòng)輪播,這樣就變成了類(lèi)似圖片輪播的效果2013-03-03

