http://jsbin.com/urubev/9/edit
在你的HTML中,你在选项值中放置了一些JavaScript代码。它们需要更改为以下内容:
<select id="dtool">
<option value="pencil">Pencil</option>
<option value="spray">Spray</option>
</select>
在JS中,我只是将您已经提供的代码片段移动到了spray对象的内部。
var outlineImage = new Image();
radius = 15
var colorPurple = "#cb3594";
var colorGreen = "#659b41";
var colorYellow = "#ffcf33";
var colorBlack = "#000000";
var CurrentColor = colorBlack;
function changeColor( color_code ) {
CurrentColor =color_code;
}
function clearCanvas(){
context.clearRect(0, 0, canvas.width, canvas.height);
}
if (window.addEventListener) {
window.addEventListener('load', function () {
var canvas, context;
var tool;
var tool_default = 'spray';
function init() {
canvas = document.getElementById('imageView');
if (!canvas) {
alert('Error: I cannot find the canvas element!');
return;
}
if (!canvas.getContext) {
alert('Error: no canvas.getContext!');
return;
}
context = canvas.getContext('2d');
if (!context) {
alert('Error: failed to getContext!');
return;
}
var tool_select = document.getElementById('dtool');
if (!tool_select) {
alert('Error: failed to get the dtool element!');
return;
}
tool_select.addEventListener('change', ev_tool_change, false);
if (tools[tool_default]) {
tool = new tools[tool_default]();
tool_select.value = tool_default;
}
canvas.addEventListener('mousedown', ev_canvas, false);
canvas.addEventListener('mousemove', ev_canvas, false);
canvas.addEventListener('mouseup', ev_canvas, false);
}
function ev_tool_change (ev) {
if (tools[this.value]) {
tool = new tools[this.value]();
}
}
var tools = {};
tools.pencil = function() {
var tool = this;
this.started = false;
this.mousedown = function (ev) {
context.beginPath();
context.strokeStyle = CurrentColor;
context.moveTo(ev._x, ev._y);
tool.started = true;
};
this.mousemove = function (ev) {
if (tool.started) {
context.strokeStyle = CurrentColor;
context.lineTo(ev._x, ev._y);
context.stroke();
}
};
this.mouseup = function (ev) {
if (tool.started) {
tool.mousemove(ev);
tool.started = false;
}
};
}
tools.spray = function() {
var tool = this;
this.started = false;
this.mousedown = function (ev) {
context.beginPath();
context.strokeStyle = CurrentColor;
context.moveTo(ev._x, ev._y);
tool.started = true;
};
this.mousemove = function (ev) {
if (tool.started) {
context.strokeStyle = CurrentColor;
context.arc(
ev._x + Math.cos( Math.random() * Math.PI * 2 ) * radius * Math.random(),
ev._y + Math.sin( Math.random() * Math.PI * 2 ) * radius * Math.random(),
1,
0, Math.PI * 2, false
);
context.fill();
}
};
this.mouseup = function (ev) {
if (tool.started) {
tool.mousemove(ev);
tool.started = false;
}
};
}
function ev_canvas(ev) {
if (navigator.appName == 'Microsoft Internet Explorer' || navigator.vendor == 'Google Inc.' || navigator.vendor == 'Apple Computer, Inc.') {
ev._x = ev.offsetX;
ev._y = ev.offsetY;
} else if (ev.layerX || ev.layerX == 0) {
ev._x = ev.layerX - this.offsetLeft;
ev._y = ev.layerY - this.offsetTop;
} else if (ev.offsetX || ev.offsetX == 0) {
ev._x = ev.offsetX;
ev._y = ev.offsetY;
}
var func = tool[ev.type];
if (func) {
func(ev);
}
}
init();
}, false);
}