原生JS+HTML5實(shí)現(xiàn)的可調(diào)節(jié)寫(xiě)字板功能示例
本文實(shí)例講述了原生JS+HTML5實(shí)現(xiàn)的可調(diào)節(jié)寫(xiě)字板功能。分享給大家供大家參考,具體如下:
前面一篇介紹了《JS+HTML5 Canvas實(shí)現(xiàn)簡(jiǎn)單的寫(xiě)字板功能》,這里再介紹另一種實(shí)現(xiàn)方法。首先來(lái)看看運(yùn)行效果:

具體代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>www.dhdzp.com JS寫(xiě)字板</title>
<style type="text/css">
#cans{
display: block;
margin: 0 auto;
}
#selectColor{
margin: 0 auto;
width: 400px;
/* height: 50px;*/
text-align: center;
margin-top: 10px;
list-style: none;
}
.all{
/*width: 10%;
height: 95%;*/
width: 60px;
height: 50px;
border: 1px solid #CCCCCC;
float: left;
margin-left: 10px;
border-radius:6px ;
cursor: pointer;
}
#red{
background-color: red;
}
#yellow{
background: yellow;
}
#black{
background: black;
}
#green{
background: green;
}
#blue{
background: #0000FF;
}
#clear{
margin-left: 10px;
}
#fontW{
width: 400px;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas id="cans">
您的瀏覽器不支持該功能!
</canvas>
<ul id="selectColor">
<li class="all" id="red" onclick="changecolor('red'),ss(this)"></li>
<li class="all" id="yellow" onclick="changecolor('yellow'),ss(this)"></li>
<li class="all" id="black" onclick="changecolor('black'),ss(this)"></li>
<li class="all" id="green" onclick="changecolor('green'),ss(this)"></li>
<li class="all" id="blue" onclick="changecolor('blue'),ss(this)"></li>
</ul></br>
<input id="clear" type="button" value="重畫(huà)"> <!--<onclick="clears()">-->
</br></br>
<div id="fontW">
<strong>線條粗細(xì):</strong>0<input type="range" id="ranges" min="0" max="20" onchange="changeW()" value="3"/>20
</div>
<script type="text/javascript">
//初始化區(qū)
var ranges=document.getElementById("ranges");
var cans=document.getElementById("cans");
var clear=document.getElementById("clear");
cans.width=600;
cans.height=600;
var down=false;
var up=false;
var clears1=false;
var lastmousePos={x:0,y:0};
var curmousePos={x:0,y:0};
var lastWidth;
var curWidth=3;
//設(shè)置canvas寬高
var CanvasWidth=cans.width;
var CanvasHeight=cans.height;
var half_width=CanvasWidth/2;
var half_height=CanvasHeight/2;
var frameW=10;
var lineW=1;
//改變線條寬度
function changeW(){
curWidth=ranges.value;
}
var drawCol="black";
//改變字體顏色
function changecolor(s){
drawCol=s;
}
var cts=cans.getContext("2d");
//設(shè)置米字格子
//開(kāi)始繪制前設(shè)置基本樣式
drawcanvs()
function drawcanvs(){
cts.lineCap="round"
cts.lineJoin="round"
cts.lineWidth=frameW;
cts.strokeStyle="red"
cts.beginPath()
cts.moveTo(0,0);
cts.lineTo(CanvasWidth,0);
cts.lineTo(CanvasWidth,CanvasHeight);
cts.lineTo(0,CanvasHeight);
cts.closePath()
cts.stroke()
//畫(huà)x
cts.lineWidth=lineW;
cts.beginPath()
cts.moveTo(0,0);
cts.lineTo(CanvasWidth,CanvasHeight);
cts.closePath()
cts.stroke()
cts.beginPath()
cts.moveTo(CanvasWidth,0);
cts.lineTo(0,CanvasHeight);
cts.closePath()
cts.stroke()
cts.beginPath()
cts.moveTo(half_width,0);
cts.lineTo(half_width,CanvasHeight);
cts.closePath()
cts.stroke()
cts.beginPath()
cts.moveTo(0,half_height);
cts.lineTo(CanvasWidth,half_height);
cts.closePath()
cts.stroke()
}
cans.onmousemove=function(e){
var e=e||event;
var clx=e.clientX-cans.getBoundingClientRect().left;//將鼠標(biāo)原點(diǎn)化為畫(huà)布原點(diǎn)
var cly=e.clientY-cans.getBoundingClientRect().top;
curmousePos={x:clx,y:cly};
if(down){
//開(kāi)始畫(huà)
//設(shè)置畫(huà)筆顏色、粗細(xì)等
cts.lineCap="round"
cts.lineJoin="round"
cts.lineWidth=curWidth;
cts.strokeStyle=drawCol;
//開(kāi)始繪畫(huà)
cts.save();
cts.beginPath();
cts.moveTo(lastmousePos.x,lastmousePos.y);
cts.lineTo(curmousePos.x,curmousePos.y);
cts.closePath();
cts.stroke();
cts.restore();
lastmousePos=curmousePos;
}
if(up){
down=false;
up=false;
}
}
//鼠標(biāo)按下事件
cans.onmousedown=function(e){
var e=e||event;
e.preventDefault();
lastmousePos=curmousePos
down=true;
}
//鼠標(biāo)松開(kāi)事件
cans.onmouseup=function(e){
var e=e||event;
e.preventDefault();
up=true;
}
//計(jì)算速度
// function calV(s,t){
// var v=s/t;
// if(v<0.1){
// curWidth=10;
// }else if(v>10){
// curWidth=2;
// }else{
// curWidth=5
// }
// }
function changeBg(){
var all=document.getElementsByClassName("all");
for(var i=0;i<all.length;i++){
all[i].onmouseover=function(){
this.style.cssText="margin-top:-6px;box-shadow:3px 5px 5px black"
}
all[i].onmouseout=function(){
this.style.cssText="border: 1px solid #CCCCCC;"
}
}
}
changeBg()
//恢復(fù)原狀
function ss(s){
s.style.cssText="margin-top:0px;box-shadow:none;"
}
//清除方法
clear.onmousedown=function (){
cts.clearRect(0,0,cans.width,cans.height);
// clears1=true;
}
//清除后重畫(huà)
clear.onmouseup=function(){
drawcanvs();
}
</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ì)有所幫助。
相關(guān)文章
JavaScript中你不知道的Object.entries用法
大家應(yīng)該都知道,Object.entries()方法返回一個(gè)給定對(duì)象自身可枚舉屬性的鍵值對(duì)數(shù)組,其排列與使用 for...in 循環(huán)遍歷該對(duì)象時(shí)返回的順序一致,這篇文章主要給大家介紹了關(guān)于JavaScript中你不知道的Object.entries用法的相關(guān)資料,需要的朋友可以參考下2021-10-10
js實(shí)現(xiàn)的簡(jiǎn)練高效拖拽功能示例
這篇文章主要介紹了js實(shí)現(xiàn)的簡(jiǎn)練高效拖拽功能,通過(guò)對(duì)js鼠標(biāo)事件的擴(kuò)展實(shí)現(xiàn)拖拽效果,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-12-12
JS實(shí)現(xiàn)上傳圖片的三種方法并實(shí)現(xiàn)預(yù)覽圖片功能
在用戶注冊(cè)頁(yè)面,需要用戶在本地選擇一張圖片作為頭像,并同時(shí)預(yù)覽,實(shí)現(xiàn)思路有兩種,具體實(shí)現(xiàn)方法和實(shí)例代碼大家參考下本文2017-07-07
TypeScript接口和類(lèi)型的區(qū)別小結(jié)
在 TypeScript 中,有兩種主要的定義自定義類(lèi)型的方式,接口和類(lèi)型,盡管它們?cè)谕庥^上可能相似,但它們之間有一些關(guān)鍵的區(qū)別,本文就詳細(xì)的介紹一下,感興趣的可以了解下2023-05-05
JavaScript檢查數(shù)字是否為整數(shù)或浮點(diǎn)數(shù)的方法
這篇文章主要介紹了JavaScript檢查數(shù)字是否為整數(shù)或浮點(diǎn)數(shù)的方法,涉及javascript類(lèi)型判斷的相關(guān)技巧,需要的朋友可以參考下2015-06-06
uniapp開(kāi)發(fā)小程序?qū)崿F(xiàn)滑動(dòng)頁(yè)面控制元素的顯示和隱藏效果
這篇文章主要介紹了uniapp開(kāi)發(fā)小程序?qū)崿F(xiàn)滑動(dòng)頁(yè)面控制元素的顯示和隱藏效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
bootstrapvalidator之API學(xué)習(xí)教程
這篇文章為大家分享了bootstrapvalidator之API學(xué)習(xí)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

