在畫布上按住滑鼠拖曳,就能畫出方塊;上方還能挑顏色、選圖形種類、設定圓角半徑。這個範例示範了四種圖形的画法差異,但真正的重點不是這四個圖形本身,而是「用 JavaScript 動態建立元素並控制其樣式」這套通用能力。

圓角矩形:寬高各自獨立,再用 borderRadius 給圓角。
正圓:寬高取兩軸拖曳距離的「最小值」,再把 borderRadius 設為寬度的一半。
矩形:寬高各自獨立,borderRadius 為 0。
正方形:同樣取最小值讓寬高等長。
另外程式在 onmousemove 裡對游標座標做了邊界夾制,讓圖形不會超出畫布範圍;Math.min(disY, y) 則負責處理「往左上拖」的情況,確保 top/left 永遠是兩點中較小的那一個。
<style>
*{margin:0; padding:0;}
#div{position:absolute; background:#ccc;}
.sel{ margin:30px auto; width:960px; overflow:hidden}
li{ list-style:none; float:left; width:60px; height:20px;}
#colors{ width:500px; float:left}
.selColor{ float:left}
#radius{ width:40px; height:20px;}
.red{background:red;} .yellow{background:yellow;} .blue{background:blue;}
.pink{background:pink;} .black{background:black;} .orange{background:orange;}
.green{ background:green;}
.xz{ width:340px; float:right;}
#canvas{ width:960px; height:500px; border:1px solid #ccc; margin:0 auto}
</style>
<script>
function $Id(id) { return document.getElementById(id); }
window.onload=function(){
var oCanvas=$Id('canvas');
var oRoud=$Id('roud');
var oRadius=$Id('radius');
var oCir=$Id('circle');
var oSqu=$Id('squ');
var oColors=$Id('colors');
var aColors=oColors.getElementsByTagName('li');
var color='red';
var aInputs=document.getElementsByTagName('input');
var xz='roud';
var arr=[];
// 收集所有 checkbox
for(var i=0;i<aInputs.length;i++) {
if(aInputs[i].type=='checkbox') { arr.push(aInputs[i]); }
}
// 單選行為:選一個就取消其他
for(var i=0;i<arr.length;i++) {
arr[i].onclick=function() {
if(!this.checked) { this.checked=false; }
else {
for(var j=0;j<arr.length;j++) { arr[j].checked=false; }
this.checked=true;
xz=this.value;
}
}
}
// 選擇顏色
for(var i=0;i<aColors.length;i++) {
aColors[i].onclick=function() { color=this.className; }
}
oCanvas.onmousedown=function(ev) {
if(oCanvas.setCapture) { oCanvas.setCapture(); }
for(var i=0;i<arr.length;i++) {
if(arr[i].checked) { arr[i].checked=true; xz=arr[i].value; }
}
var oEv=ev||window.event;
var disX=oEv.clientX;
var disY=oEv.clientY;
var oR=document.createElement('div');
oR.id="div";
oR.style.top=disY+'px';
oR.style.left=disX+'px';
oR.style.backgroundColor=color;
document.body.appendChild(oR);
document.onmousemove=function(ev) {
var oEv=ev||window.event;
var x=oEv.clientX;
var y=oEv.clientY;
// 邊界夾制
if(x<oCanvas.offsetLeft) { x=oCanvas.offsetLeft; }
else if(x>oCanvas.offsetLeft+oCanvas.offsetWidth) { x=oCanvas.offsetLeft+oCanvas.offsetWidth }
if(y<oCanvas.offsetTop) { y=oCanvas.offsetTop; }
else if(y>oCanvas.offsetTop+oCanvas.offsetHeight) { y=oCanvas.offsetTop+oCanvas.offsetHeight }
oR.style.width=Math.abs(x-disX)+'px';
oR.style.top=Math.min(disY,y)+'px';
oR.style.left=Math.min(disX,x)+'px';
switch(xz) {
case 'roud':
oR.style.height=Math.abs(y-disY)+'px';
oR.style.borderRadius=oRadius.value+'px';
break;
case 'circle':
oR.style.height=Math.min(Math.abs(x-disX),Math.abs(y-disY))+'px';
oR.style.width=Math.min(Math.abs(x-disX),Math.abs(y-disY))+'px';
oR.style.borderRadius=(Math.min(Math.abs(x-disX),Math.abs(y-disY)))/2+'px';
break;
case 'squ':
oR.style.height=Math.abs(y-disY)+'px';
break;
case 'square':
oR.style.height=Math.min(Math.abs(x-disX),Math.abs(y-disY))+'px';
oR.style.width=Math.min(Math.abs(x-disX),Math.abs(y-disY))+'px';
}
}
document.onmouseup=function() {
document.onmousemove=null;
document.onmouseout=null;
if(oCanvas.releaseCapture) { oCanvas.releaseCapture(); }
}
return false;
}
}
</script>
<div class="sel"> <span class="selColor">請選擇一種顏色</span> <ul id="colors"> <li value="red" class="red"></li> <li value="yellow" class="yellow"></li> <li value="blue" class="blue"></li> <li value="pink" class="pink"></li> <li value="black" class="black"></li> <li value="orange" class="orange"></li> <li value="green" class="green"></li> </ul> <p class="xz"> <input type="checkbox" value="roud" id="roud" />圓角矩形 <label>半徑</label><input type="text" value="" id="radius" /> <input type="checkbox" id="circle" value="circle" />正圓 <input type="checkbox" id="squ" value="squ" />矩形 <input type="checkbox" id="square" value="square" />正方形 </p> </div> <div id="canvas"> </div>
第一,用四個 checkbox 模擬單選其實很彎曲,還得自己寫迴圈去取消其他選項。直接改用 <input type="radio" name="shape">,瀏覽器天生就有互斥行為,那個 for 迴圈可以整段刪掉。
第二,borderRadius 的數值來自 oRadius.value,使用者若輸入非數字會產生無效樣式;建議加上 parseInt(oRadius.value, 10) || 0 保底。另外每個新建的 div 都指定 id="div",多畫幾個就會出現重複 id,應該改成 class。
如果想讓這個小工具更實用,可以再加上「填色或只畫框線」的選項、橡皮擦(點擊刪除圖形)、以及把畫布匯出成 PNG(canvas.toDataURL(),前提是改用真正的 canvas 元素而非 div)。
以上就是這個圖形繪製小工具的完整實作與改良方向。