<script type="text/javascript">
var canvas, context, tool, e;
var varblurup=0;
var varsizeup=1;
var swtchclr;
// Keep everything in anonymous function, called on window load.
if(window.addEventListener) {
window.addEventListener('load', function() {
//check tool is pen or line or shape
var chktool="pen";
function init() {
alert("Line3");
// Find the canvas element.
canvas = document.getElementById('canvas');
context = canvas.getContext('2d');
//varblurup=10;
context.shadowColor = 'colour';
context.shadowBlur = 0;
context.lineWidth=1;
context.lineJoin = 'miter';
context.miterLimit = 4;
this.context.save();
// Pencil tool instance.
//tool = new tool_pencil();
//alert("Pen");
if(chktool=="pen")
{ tool = new tool_pencil();
alert("Pen");
}else if (chktool=="line")
{
tool2 = new tool_line();
alert("Line");
}
// Attach the mousedown, mousemove and mouseup event listeners.
canvas.addEventListener('mousedown', ev_canvas, false);
canvas.addEventListener('mousemove', ev_canvas, false);
canvas.addEventListener('mouseup', ev_canvas, false);
}
// This painting tool works like a drawing pencil which tracks the mouse
// movements.
function tool_pencil() {
var tool = this;
this.started = false;
// This is called when you start holding down the mouse button.
// This starts the pencil drawing.
this.mousedown = function (ev) {
context.beginPath();
context.moveTo(ev._x, ev._y);
tool.started = true;
};
// This function is called every time you move the mouse. Obviously, it only
// draws if the tool.started state is set to true (when you are holding down
// the mouse button).
this.mousemove = function (ev) {
if (tool.started) {
context.lineTo(ev._x, ev._y);
//this.style('stroke-opacity').value
context.stroke();
}
};
// This is called when you release the mouse button.
this.mouseup = function (ev) {
if (tool.started) {
tool.mousemove(ev);
tool.started = false;
}
};
}
// The general-purpose event handler. This function just determines the mouse
// position relative to the canvas element.
function ev_canvas (ev) {
if (ev.layerX || ev.layerX == 0) { // Firefox
ev._x = ev.layerX;
ev._y = ev.layerY;
} else if (ev.offsetX || ev.offsetX == 0) { // Opera
ev._x = ev.offsetX;
ev._y = ev.offsetY;
}
// Call the event handler of the tool.
var func = tool[ev.type];
if (func) {
func(ev);
}
}
init();
}, false); }
0
A
回答
0
1
我發現Mozilla的畫布教程是一個有益的指導。它涵蓋了大部分地區,包括形狀繪製:
https://developer.mozilla.org/en/Canvas_tutorial
https://developer.mozilla.org/en/Canvas_tutorial/Drawing_shapes
相關問題
- 1. 在畫布上繪製三角形矩形時的細線/空白空間
- 2. 如何繪製畫布形狀之間的對角線
- 3. 繪製形狀時線條被擦除
- 4. 線條觸摸或矩形或圓形
- 5. 矩形線條繪製在python
- 6. 用戶如何通過觸摸在形狀上繪畫或繪製線條
- 7. android如何在畫布上繪製三角形,星形,方形,心形
- 8. 線條,圓圈和其他形狀?
- 9. OpengGL glDrawElements繪製線條,但不是三角形
- 10. 如何繪製線條動畫來勾勒各種形狀?
- 11. 我可以繪製形狀像圓,矩形,線等的drawRect方法以外
- 12. 在矩形邊上繪製相交線,繪製其中的線
- 13. 如何在畫布上繪製虛線矩形?
- 14. 如何在活動選項卡上製作三角形形狀
- 15. Chart.js如何在條形圖的畫布上添加一條線
- 16. 如何在條形圖上繪製軸之間的虛線android
- 17. 發現一個矩陣是上三角形,下三角形還是對角線
- 18. Java 1.5使用線條和角度繪製形狀的問題
- 19. 以不正確的角度繪製線條和形狀
- 20. 當繪製三角形條時,什麼控制OpenGL的行爲?
- 21. 在Android畫布上繪製直角矩形/弧形
- 22. 斑馬線條形碼掃描器無法讀取條形碼
- 23. 畫布繪製用線性漸變填充圓角矩形
- 24. Android畫布 - 繪製矩形或矩形的圖片
- 25. 繪製上述條形圖線
- 26. 在相機預覽流(.NET Compact Framework)上繪製矩形(或線條或位圖)
- 27. 在三角形的三角形中繪製三角形
- 28. 繪製矩形畫布android
- 29. 繪製畫布3d矩形
- 30. Android畫布繪製矩形