-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (94 loc) · 3.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
$(function() {
var screenWidth = document.documentElement.clientWidth,
screenHeight = document.documentElement.clientHeight,
canvasWidth = 1200,
canvasHeight = screenHeight - 200, //canvas标签的大小变量
canvasDiv = document.getElementById('canvasDiv'), //canvas容器
canvas = document.createElement('canvas'), //创建了元素canvas
crayonTextureImage = new Image(); //新建图片对象用于蜡笔样式
/* 设置canvas属性 */
canvas.setAttribute('width', canvasWidth);
canvas.setAttribute('height', canvasHeight);
canvas.setAttribute('id', 'canvas');
canvasDiv.appendChild(canvas); //为canvasDiv增加了子元素canvas
/* 设置蜡笔样式图片路径 */
crayonTextureImage.src = "assets/images/crayon-texture.png";
/* 解决IE浏览器不支持html5 canvas:通过excanvas.js专门为IE扩展的canvas元素包中提供的处理方法initElement进行相应的判断处理 */
if (typeof G_vmlCanvasManager != 'undefined') {
canvas = G_vmlCanvasManager.initElement(canvas);
}
/* 使用canvas的绘图功能:调用canvas的上下文 */
context = canvas.getContext("2d");
/* 绘画变量 */
var draw, x, y, w, h,
canvasArr = [],
type = 'pen',
color = '#000',
style = 'stroke',
lineWidth = '1';
/* 绘画形状 */
$('.line-shape').on('click', 'a', function() {
$(this).addClass('active').siblings('.active').removeClass('active');
type = $(this).attr('data-use');
if (type === 'line' || type === 'pen') {
style = 'stroke';
$('.style .stroke').addClass('active').siblings('.fill').removeClass('active').hide();
} else {
$('.style .fill').show();
}
});
/* 描边 || 填充 */
$('.style').on('click', 'a', function() {
style = $(this).attr('data-use');
$(this).addClass('active').siblings().removeClass('active');
});
/* 线宽 */
$('.line-width').on('change', function() {
lineWidth = $(this).val();
});
$('.line-size').on('click', '.size', function() {
lineWidth = $(this).attr('data-size');
});
/* 颜色 */
$('.input-color').on('change', function() {
color = $(this).val();
});
$('.color-choice').on('click', 'span', function() {
color = $(this).attr('class');
});
/* 清空画板 */
$('.clearCanvas').on('click', function() {
canvasArr = [];
context.clearRect(0, 0, canvasWidth, canvasHeight);
})
/* 鼠标点击事件 */
canvas.onmousedown = function(e) {
x = e.offsetX;
y = e.offsetY;
if (type == "pen") {
context.beginPath();
context.moveTo(x, y);
}
if (type == "eraser") {
context.clearRect(x - 5, y - 5, 10, 10);
}
var draw = new Draw(context, { type: style, color: color, width: lineWidth }); //实例化构造函数
canvas.onmousemove = function(e) {
w = e.offsetX;
h = e.offsetY;
/* 清除canvas画布显示最后画布的状态 */
if (type != "eraser") {
context.clearRect(0, 0, canvasWidth, canvasHeight);
if (canvasArr.length != 0) {
context.putImageData(canvasArr[canvasArr.length - 1], 0, 0, 0, 0, canvasWidth, canvasHeight);
}
}
draw[type](x, y, w, h);
}
canvas.onmouseup = function() {
canvas.onmousemove = null;
document.onmouseup = null;
canvasArr.push(context.getImageData(0, 0, canvasWidth, canvasHeight));
}
}
})