forked from geraintluff/canvas-sketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
177 lines (170 loc) · 5.92 KB
/
index.html
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
<style>
canvas {
border: 1px solid black;
border-radius: 5px;
margin-right: 1em;
}
</style>
Drag and drop an image into this box to process it:<br>
<canvas id="target"></canvas><br>
Greyscale: <input type="checkbox" id="greyscale" checked><br>
Level steps: <select id="levelSteps">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6" selected>6</option>
</select><br>
Line alpha: <select id="lineAlpha">
<option value="0.05">0.05</option>
<option value="0.1" selected>0.1</option>
<option value="0.2">0.2</option>
<option value="0.5">0.5</option>
</select><br>
Line thickness: <select id="lineThickness">
<option value="0.5">0.5</option>
<option value="1">1</option>
<option value="2" selected>2</option>
<option value="3">3</option>
<option value="5">5</option>
<option value="10">10</option>
</select><br>
Line density: <select id="lineDensity">
<option value="0.25">0.25</option>
<option value="0.5" selected>0.5</option>
<option value="1">1</option>
</select><br>
Lightness: <select id="lightness">
<option value="1">1</option>
<option value="2">2</option>
<option value="4" selected>4</option>
<option value="8">8</option>
</select><br>
Edge thickness: <select id="edgeThickness">
<option value="2" selected>2</option>
<option value="3">3</option>
<option value="5">5</option>
<option value="10">10</option>
</select><br>
Edge amount: <select id="edgeAmount">
<option value="0">0</option>
<option value="0.5" selected>0.5</option>
<option value="1">1</option>
<option value="2">2</option>
</select><br>
<hr>
Base64 value:<Br>
<textarea id="base64-data" style="width: 100%; height: 5em;"></textarea><br>
<a href="#" id="base64-link">Open in new window</a>
<script src="sketch.js"></script>
<script src="jszip.js"></script>
<script>
document.title = "Drag and drop image to convert";
document.getElementById("base64-link").onclick = function () {
window.open(this.getAttribute("href"));
return false;
};
var canvas = document.getElementById("target");
canvas.addEventListener('dragover', function (evt) {
evt.preventDefault();
}, false);
canvas.addEventListener('drop', getfiles, false);
function getfiles(ev) {
ev.preventDefault();
var files = [];
for (var i = 0; i < ev.dataTransfer.files.length; i++) {
files.push(ev.dataTransfer.files[i]);
}
if (files.length == 1) {
var file = files[0];
if (file.type.indexOf('image') === -1 ) {
return;
}
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function ( ev ) {
var img = new Image();
img.src = ev.target.result;
img.onload = function() {
document.getElementById("base64-data").value = "Processing...";
processImage(img);
};
};
} else if (files.length > 1) {
var frameZip = new JSZip();
var imageCounter = 0;
function processNext() {
if (files.length > 0) {
var file = files.shift();
var filename = file.name;
document.title = "Processing: " + filename;
document.getElementById("base64-data").value = "Processing: " + filename;
if (file.type.indexOf('image') === -1 ) {
processNext();
}
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function ( ev ) {
var img = new Image();
img.src = ev.target.result;
img.onload = function() {
var sketcher = processImage(img, imageCounter != 0);
sketcher.whenReady(function () {
imageCounter++;
filename = filename.split(".");
filename = filename.slice(0, filename.length - 1);
filename = filename.join(".") + ".png";
var dataUrl = canvas.toDataURL();
var base64 = dataUrl.replace(/^data:image\/\w+;base64,/, "");
frameZip.file(filename, base64, {base64: true});
window.setTimeout(processNext, 100);
});
};
};
} else if (imageCounter > 0) {
var content = frameZip.generate();
document.getElementById("base64-data").value = content;
document.getElementById("base64-link").setAttribute("href", "#");
}
}
processNext();
}
};
function processImage(img, noInitialPreview) {
var context = canvas.getContext("2d");
if (img.width != canvas.width || img.height != canvas.height) {
canvas.width = img.width;
canvas.height = img.height;
noInitialPreview = false;
}
if (!noInitialPreview) {
context.drawImage(img, 0, 0, canvas.width, canvas.height);
}
var sketcher = new Sketcher(canvas.width, canvas.height);
sketcher.levelSteps = parseFloat(document.getElementById('levelSteps').value);
sketcher.lineAlpha = parseFloat(document.getElementById('lineAlpha').value);
sketcher.lineThickness = parseFloat(document.getElementById('lineThickness').value);
sketcher.lineDensity = parseFloat(document.getElementById('lineDensity').value);
sketcher.lightness = parseFloat(document.getElementById('lightness').value);
sketcher.edgeBlurAmount = parseFloat(document.getElementById('edgeThickness').value);
sketcher.edgeAmount = parseFloat(document.getElementById('edgeAmount').value);
sketcher.progressUpdate(function (proportion, message) {
console.log((Math.round(proportion*1000)/10) + "% done - " + message);
document.title = (Math.round(proportion*1000)/10) + "% done - " + message;
});
var greyscale = !!document.getElementById("greyscale").checked;
document.getElementById("base64-link").setAttribute('href', "#");
document.title = "Transforming canvas...";
console.log("Transforming canvas...");
context.drawImage(img, 0, 0, canvas.width, canvas.height);
sketcher.transformCanvas(canvas, greyscale).whenReady(function () {
console.log("Done!");
document.title = "Sketch completed";
var dataUrl = canvas.toDataURL();
document.getElementById("base64-link").setAttribute('href', dataUrl);
var base64 = dataUrl.replace(/^data:image\/\w+;base64,/, "");
document.getElementById("base64-data").value = base64;
});
return sketcher;
}
</script>