-
Notifications
You must be signed in to change notification settings - Fork 0
/
cas.js
220 lines (204 loc) · 7.38 KB
/
cas.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* Object Helpers
**/
function o(b) {
var res = {};
for (var key in b)
if (key.substr(0, 2) === "__")
Object.defineProperty(res, key.substr(2), { value: b[key] });
else if (key.charAt(0) === "_")
Object.defineProperty(res, key.substr(1), { value: b[key], writeable: true, configurable: true });
else
res[key] = (typeof b[key] === "object")? o(b[key]) : b[key];
return res;
}
function l(k, d) {
var res = JSON.parse(localStorage.getItem(k)) || {};
(function deepSet(t, s) {
for (var key in s) {
if (!(key in t)) {
if (typeof s[key] === "object" && key.charAt(0) !== "_") {
deepSet(t[key] = {}, s[key]);
} else {
t[key] = s[key];
}
} else if ((typeof t[key]) === "object" && (typeof s[key]) === "object") {
deepSet(t[key], s[key]);
}
}
})(res, d);
return res;
}
/**
* HTML BULL
**/
function app(p) { for (var i = 1; i < arguments.length; ++i) p.appendChild(arguments[i]); return p; }
function on(ev, f, el) { el.addEventListener(ev, f); return el; }
var div = elt.bind(this, "div");
var H = {
Box: function (title) {
var bax = div({class:"bax"}),
res = div({class:"box"}, elt("h2", null, title), bax);
for (var i = 1; i < arguments.length; ++i)
app(bax, arguments[i]);
return res;
},
ImageFileInput: function (img) {
var res = elt("input", {type:"file"});
res.onchange = function () {
var file = res.files[0];
if (!(/^image\//.test(file.type))) { return; }
var reader = new FileReader();
reader.onload = function (e) { img.src = e.target.result; };
reader.readAsDataURL(file);
}
return res;
},
Label: function (label, input) {
return elt("label", null, label+": ", input);
},
Link: function (label, href) {
return elt("a", {href:href,target:"_blank"}, label);
},
twbool: function (obj, key, onchange) {
var val = obj[key],
res = elt("input", {type:"checkbox"});
res.checked = val;
prop(obj, key, val, function (v) {
res.checked = v;
return v;
});
res.onchange = function () {
obj[key] = res.checked;
onchange && onchange(obj[key]);
};
return res;
},
twint: function (obj, key, oninput) {
var val = obj[key],
res = elt("input", {type:"number", value:val});
prop(obj, key, val, function (v) {
res.value = v;
return v;
});
res.oninput = function () {
obj[key] = parseInt(res.value, 10);
oninput && oninput(obj[key]);
};
return res;
},
};
/**
* Application Cord
**/
function cropAndScale(stort) {
var ctx = stort.tmpCanvas.getContext("2d"), src = stort.src, dst = stort.dst;
if (dst.mode === "size") {
ctx.canvas.width = dst.width;
ctx.canvas.height = dst.height;
} else { // if (dst.mode === "zoom") {
ctx.canvas.width = src.width * dst.zoom;
ctx.canvas.height = src.height * dst.zoom;
}
// need to set this after changing canvas dimensions, apparently
ctx.mozImageSmoothingEnabled = dst.smoothing;
ctx.webkitImageSmoothingEnabled = dst.smoothing;
ctx.msImageSmoothingEnabled = dst.smoothing;
ctx.imageSmoothingEnabled = dst.smoothing;
if (src.mode === "size") {
ctx.drawImage(src.img, src.x, src.y, src.width, src.height,
0, 0, ctx.canvas.width, ctx.canvas.height);
dst.img.src = ctx.canvas.toDataURL();
} else {
alert("You should not be seeing this.");
}
setTimeout(function saveSettings() {
localStorage.setItem("cas-settings", JSON.stringify(stort));
}, 0);
}
function redraw(stort, scale) {
var src = stort.src, img = src.img, can = stort.selectionCanvas, ctx = can.getContext("2d"),
scale = scale || (src.img.width / src.img.naturalWidth);
can.width = img.width;
can.height = img.height;
ctx.fillStyle = "rgba(0,0,0,.5)";
ctx.fillRect(0, 0, can.width, can.height);
ctx.clearRect(src.x * scale, src.y * scale, src.width * scale, src.height * scale);
}
function onImgLoad(stort) {
$("#main").classList.add("img-loaded");
redraw(stort);
cropAndScale(stort);
}
function setSelection(stort, e) {
if (!stort.src.img.width) { return; }
if (!(e.buttons & 1)) { return; }
var pos = mouse(e, stort.selectionCanvas), src = stort.src, scale = src.img.width / src.img.naturalWidth;
pos.x /= scale;
pos.y /= scale;
src.x = Math.min(Math.max(0, Math.round(Math.round(pos.x - src.width / 2) / src.snap) * src.snap), src.img.naturalWidth - src.width);
src.y = Math.min(Math.max(0, Math.round(Math.round(pos.y - src.height / 2) / src.snap) * src.snap), src.img.naturalHeight - src.height);
redraw(stort, scale);
}
/**
* Let's do this
**/
window.onload = function () {
var stort = o(l("cas-settings", {
src: {
mode: "size",
_x: 0,
_y: 0,
width: 150,
height: 150,
snap: 2,
zoom: .5,
__img: elt("img"),
},
dst: {
mode: "zoom",
width: 300,
height: 300,
zoom: 2,
smoothing: false,
__img: elt("img"),
},
__main: div({id:"main"}),
__selectionCanvas: elt("canvas", {id:"selection"}),
__tmpCanvas: elt("canvas"),
})),
crorpAndScorle = cropAndScale.bind(this, stort),
redrorw = redraw.bind(this, stort),
wholeShebang = function () { redrorw(); crorpAndScorle(); };
app(document.body,
div(null,
elt("h1", null, "Crop And Scale ", elt("span", {class:"subtitle"}, "[\u25A3 + \u00D7]")),
app(stort.main,
H.Box("Step 1: Pick Your Image.",
H.Label("Source File", H.ImageFileInput(stort.src.img))),
H.Box("Step 2: Set Your Crop Box.",
div({class:"img-wrapper"},
on("load", onImgLoad.bind(this, stort), stort.src.img),
on("mousedown", setSelection.bind(this, stort),
on("mousemove", setSelection.bind(this, stort),
on("mouseup", crorpAndScorle,
on("touchstart", function (e) { setSelection(stort, e.changedTouches[0]); },
on("touchmove", function (e) { setSelection(stort, e.changedTouches[0]); },
on("touchend", crorpAndScorle,
stort.selectionCanvas))))))),
H.Label("Source X", H.twint(stort.src, "x", wholeShebang)),
H.Label("Source Y", H.twint(stort.src, "y", wholeShebang)),
H.Label("Source Width", H.twint(stort.src, "width", wholeShebang)),
H.Label("Source Height", H.twint(stort.src, "height", wholeShebang)),
H.Label("Source Snap", H.twint(stort.src, "snap", wholeShebang))),
H.Box("Step 3: Right Click to Save!",
stort.dst.img,
H.Label("Smooth Icon", H.twbool(stort.dst, "smoothing", crorpAndScorle)),
H.Label("Icon Scale", H.twint(stort.dst, "zoom", crorpAndScorle)))),
elt("h3", null, "Made by ",
H.Link("Daniel Bucci", "mailto:[email protected]"), " for his ",
H.Link("Super FTC", "http://www.superftc.com/comic"), " ",
H.Link("Tapastic Comic", "http://www.tapastic.com/series/Super-FTC"), ".", elt("br"),
H.Link("Fork me on GortHorb.", "https://github.com/dlbucci/crop-and-scale"))));
window.onresize = redrorw;
};