forked from yurafuca/5000choyen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.js
More file actions
134 lines (117 loc) · 3.92 KB
/
canvas.js
File metadata and controls
134 lines (117 loc) · 3.92 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* globals SETTINGS, Drawer */
var Canvas = function(canvas) {
this.canvas = canvas;
this.canvas.addEventListener('mousedown', this.onDown.bind(this), false);
this.canvas.addEventListener('mousemove', this.onMove.bind(this), false);
this.canvas.addEventListener('mouseup', this.onUp.bind(this), false);
this.canvas.addEventListener('touchstart', this.onTouchStart.bind(this), false);
this.canvas.addEventListener('touchmove', this.onTouchMove.bind(this), false);
this.canvas.addEventListener('touchend', this.onTouchEnd.bind(this), false);
this.ctx = canvas.getContext('2d');
this.ctx.lineJoin = 'round';
this.ctx.lineCap = 'round';
this.ctx.fillStyle = 'white';
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
this.offset = {
top: { x: 0, y: 0 },
bottom: { x: 250, y: 130 }
};
this.dragging = false;
this.dragPosition = { x0: 0, y0: 0 };
this.drawer = new Drawer(this.ctx);
}
Canvas.prototype.onDown = function(e) {
this.dragging = true;
this.dragPosition.x0 = e.clientX;
this.dragPosition.y0 = e.clientY;
};
Canvas.prototype.onMove = function(e) {
const dx = e.clientX - this.dragPosition.x0;
const x = this.offset.bottom.x + dx;
if (this.dragging) {
if (SETTINGS.TEXT_ORDER() === `image`)
this.redrawImage(x);
else
this.redrawBottom(x);
}
if (this.upperEndPosition() < e.clientY && e.clientY < this.lowerEndPosition())
document.body.style.cursor = "move";
else
document.body.style.cursor = "auto"
};
Canvas.prototype.onUp = function(e) {
var dx = e.clientX - this.dragPosition.x0;
this.offset.bottom.x += dx;
this.dragging = false;
};
Canvas.prototype.onTouchStart = function(e) {
e.preventDefault();
e.clientX = e.touches[0].clientX;
e.clientY = e.touches[0].clientY;
this.onDown(e);
};
Canvas.prototype.onTouchMove = function(e) {
e.preventDefault();
e.clientX = e.touches[0].clientX;
e.clientY = e.touches[0].clientY;
this.onMove(e);
};
Canvas.prototype.onTouchEnd = function(e) {
e.preventDefault();
e.clientX = e.changedTouches[0].clientX;
e.clientY = e.changedTouches[0].clientY;
this.onUp(e);
};
Canvas.prototype.upperEndPosition = function() {
return this.canvas.getBoundingClientRect().top + this.offset.bottom.y;
}
Canvas.prototype.lowerEndPosition = function() {
return this.canvas.getBoundingClientRect().top + (this.canvas.height - 10);
}
Canvas.prototype.redrawTop = function () {
const text = document.getElementById("textboxTop").value;
const x = 70;
const y = 100;
const order = SETTINGS.BACKGROUND_ORDER();
this.drawer.redrawTop(text, x, y, order);
if (SETTINGS.TEXT_ORDER() === "image")
this.redrawImage();
else
this.redrawBottom();
}
Canvas.prototype.redrawBottom = function (offsetX) {
const text = document.getElementById("textboxBottom").value.replace(/!/, `!`);
const x = (offsetX || this.offset.bottom.x) + 70;
const y = this.offset.bottom.y + 100;
const order = SETTINGS.BACKGROUND_ORDER();
this.drawer.redrawBottom(text, x, y, order);
}
Canvas.prototype.redrawImage = function(offsetX) {
const x = (offsetX || this.offset.bottom.x) + 70;
const y = this.offset.bottom.y;
const order = SETTINGS.BACKGROUND_ORDER();
this.drawer.redrawImage(x, y, order);
}
Canvas.prototype.save = function() {
this.drawer.save();
}
Canvas.prototype.newtab = function() {
const order = SETTINGS.TEXT_ORDER();
const color = SETTINGS.BACKGROUND_ORDER();
const top = document.getElementById("textboxTop").value;
const bottom = document.getElementById("textboxBottom").value.replace(/!/, `!`);
const tx = 70;
const ty = 100;
const bx = this.offset.bottom.x + 70;
const by = this.offset.bottom.y + (order === `text` ? 100 : 0);
const q =
'top=' + top +
'&bottom=' + bottom +
'&tx=' + tx +
'&ty=' + ty +
'&bx=' + bx +
'&by=' + by +
'&order=' + order +
'&color=' + color;
this.drawer.newtab(q);
}