forked from drskullster/p5.js-export
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathp5.js-export.js
423 lines (353 loc) · 12.2 KB
/
p5.js-export.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
ISO_PORTRAIT_RATIO = 0.7071;
ISO_LANDSCAPE_RATIO = 1.4142;
const sizes = {
a: [
[841, 1189],
[594, 841],
[420, 594],
[297, 420],
[210, 297],
[148, 210],
[105, 148],
[74, 105],
[52, 74],
[37, 52],
[26, 37]
]
};
saveForPrint = function(_fileName, _size, _resolution, _savedFrames) {
const fileName = _fileName ? _fileName : "export.jpg";
const reencodePNG = fileName.indexOf(".png") > -1;
const resolution = _resolution ? _resolution : 300;
const savedFrames = _savedFrames ? _savedFrames : 1;
let size = sizes.a[4];
if (_size) {
const letter = _size[0].toLowerCase();
const num = _size[1];
size = sizes[letter][num];
}
const pixelDensity = _renderer._pInst.pixelDensity();
const origW = width;
const origH = height;
const destW = getPxSize(size[0], resolution);
const destH = getPxSize(size[1], resolution);
saveCanvasProperties();
_renderer.elt.width = destW;
_renderer.elt.height = destH;
_renderer.width = destW;
_renderer.height = destH;
resetCanvasProperties();
scale(destW / origW);
for (var i = 0; i < savedFrames; i++) {
draw();
}
if (reencodePNG) saveReEncodedPng(fileName, resolution)
else save(fileName);
_renderer.elt.width = width * pixelDensity;
_renderer.elt.height = height * pixelDensity;
_renderer.width = origW;
_renderer.height = origH;
resetCanvasProperties();
scale(1);
};
function getPxSize(_size, _resolution) {
return parseInt((_size / 10) * (_resolution / 2.54));
}
/**
* resizing canvas somehow removes properties from context.
* So we need to save all properties before resize,
* then reset them. Borrowed from p5.js resizeCanvas()
* source code.
**/
let props;
function saveCanvasProperties() {
props = {};
for (let key in _renderer.drawingContext) {
let val = _renderer.drawingContext[key];
if (typeof val !== 'object' && typeof val !== 'function') {
props[key] = val;
}
}
}
function resetCanvasProperties() {
for (var savedKey in props) {
try {
_renderer.drawingContext[savedKey] = props[savedKey];
} catch (err) {
// ignore read-only property errors
}
}
}
function saveReEncodedPng(fileName, resolution) {
let pngHeader = "data:image/png;base64,";
let base64 = _renderer.canvas.toDataURL().substr(pngHeader.length);
let binary_string = window.atob(base64);
let len = binary_string.length;
let bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
// pixels per metre
let ppm = Math.round(resolution / 2.54 * 100);
bytes = rewrite_pHYs_chunk(bytes, ppm, ppm);
// re-encode PNG (btoa method)
var b64encoded = btoa(
new Uint8Array(bytes)
.reduce((data, byte) => data + String.fromCharCode(byte), '')
);
// download file
let a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = pngHeader + b64encoded;
a.download = fileName;
a.click();
}
// source: https://github.com/murkle/rewrite-png-pHYs-chunk
/* adapted from https://github.com/hughsk/png-chunks-extract (MIT) and https://github.com/SheetJS/js-crc32 (Apache 2.0) (C) 2014-present SheetJS -- http://sheetjs.com */
let rewrite_pHYs_chunk = function (data, ppmx, ppmy) {
/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
/* vim: set ts=2: */
/*exported CRC32 */
var CRC32;
(function (factory) {
/*jshint ignore:start */
if (typeof DO_NOT_EXPORT_CRC === 'undefined') {
if ('object' === typeof exports) {
factory(exports);
} else if ('function' === typeof define && define.amd) {
define(function () {
var module = {};
factory(module);
return module;
});
} else {
factory(CRC32 = {});
}
} else {
factory(CRC32 = {});
}
/*jshint ignore:end */
}(function (CRC32) {
CRC32.version = '1.1.1';
/* see perf/crc32table.js */
/*global Int32Array */
function signed_crc_table() {
var c = 0, table = new Array(256);
for (var n = 0; n != 256; ++n) {
c = n;
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
table[n] = c;
}
return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
}
var T = signed_crc_table();
function crc32_bstr(bstr, seed) {
var C = seed ^ -1, L = bstr.length - 1;
for (var i = 0; i < L;) {
C = (C >>> 8) ^ T[(C ^ bstr.charCodeAt(i++)) & 0xFF];
C = (C >>> 8) ^ T[(C ^ bstr.charCodeAt(i++)) & 0xFF];
}
if (i === L) C = (C >>> 8) ^ T[(C ^ bstr.charCodeAt(i)) & 0xFF];
return C ^ -1;
}
function crc32_buf(buf, seed) {
if (buf.length > 10000) return crc32_buf_8(buf, seed);
var C = seed ^ -1, L = buf.length - 3;
for (var i = 0; i < L;) {
C = (C >>> 8) ^ T[(C ^ buf[i++]) & 0xFF];
C = (C >>> 8) ^ T[(C ^ buf[i++]) & 0xFF];
C = (C >>> 8) ^ T[(C ^ buf[i++]) & 0xFF];
C = (C >>> 8) ^ T[(C ^ buf[i++]) & 0xFF];
}
while (i < L + 3) C = (C >>> 8) ^ T[(C ^ buf[i++]) & 0xFF];
return C ^ -1;
}
function crc32_buf_8(buf, seed) {
var C = seed ^ -1, L = buf.length - 7;
for (var i = 0; i < L;) {
C = (C >>> 8) ^ T[(C ^ buf[i++]) & 0xFF];
C = (C >>> 8) ^ T[(C ^ buf[i++]) & 0xFF];
C = (C >>> 8) ^ T[(C ^ buf[i++]) & 0xFF];
C = (C >>> 8) ^ T[(C ^ buf[i++]) & 0xFF];
C = (C >>> 8) ^ T[(C ^ buf[i++]) & 0xFF];
C = (C >>> 8) ^ T[(C ^ buf[i++]) & 0xFF];
C = (C >>> 8) ^ T[(C ^ buf[i++]) & 0xFF];
C = (C >>> 8) ^ T[(C ^ buf[i++]) & 0xFF];
}
while (i < L + 7) C = (C >>> 8) ^ T[(C ^ buf[i++]) & 0xFF];
return C ^ -1;
}
function crc32_str(str, seed) {
var C = seed ^ -1;
for (var i = 0, L = str.length, c, d; i < L;) {
c = str.charCodeAt(i++);
if (c < 0x80) {
C = (C >>> 8) ^ T[(C ^ c) & 0xFF];
} else if (c < 0x800) {
C = (C >>> 8) ^ T[(C ^ (192 | ((c >> 6) & 31))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | (c & 63))) & 0xFF];
} else if (c >= 0xD800 && c < 0xE000) {
c = (c & 1023) + 64; d = str.charCodeAt(i++) & 1023;
C = (C >>> 8) ^ T[(C ^ (240 | ((c >> 8) & 7))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | ((c >> 2) & 63))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | ((d >> 6) & 15) | ((c & 3) << 4))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | (d & 63))) & 0xFF];
} else {
C = (C >>> 8) ^ T[(C ^ (224 | ((c >> 12) & 15))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | ((c >> 6) & 63))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | (c & 63))) & 0xFF];
}
}
return C ^ -1;
}
CRC32.table = T;
CRC32.bstr = crc32_bstr;
CRC32.buf = crc32_buf;
CRC32.str = crc32_str;
}));
// Used for fast-ish conversion between uint8s and uint32s/int32s.
// Also required in order to remain agnostic for both Node Buffers and
// Uint8Arrays.
var uint8 = new Uint8Array(4);
var int32 = new Int32Array(uint8.buffer);
var uint32 = new Uint32Array(uint8.buffer);
var pHYsFound = false;
if (data[0] !== 0x89 || data[1] !== 0x50 || data[2] !== 0x4E || data[3] !== 0x47 || data[4] !== 0x0D || data[5] !== 0x0A || data[6] !== 0x1A || data[7] !== 0x0A) {
throw new Error('Invalid .png file header: possibly caused by DOS-Unix line ending conversion?');
}
var ended = false
var idx = 8
while (idx < data.length) {
// Read the length of the current chunk,
// which is stored as a Uint32.
uint8[3] = data[idx++]
uint8[2] = data[idx++]
uint8[1] = data[idx++]
uint8[0] = data[idx++]
// Chunk includes name/type for CRC check (see below).
var length = uint32[0] + 4
var chunk = new Uint8Array(length)
chunk[0] = data[idx++]
chunk[1] = data[idx++]
chunk[2] = data[idx++]
chunk[3] = data[idx++]
// Get the name in ASCII for identification.
var name = (
String.fromCharCode(chunk[0]) +
String.fromCharCode(chunk[1]) +
String.fromCharCode(chunk[2]) +
String.fromCharCode(chunk[3])
);
//console.log("chunk found " + name + ", length = " + (length - 4));
var chunkDataStart = idx;
// Read the contents of the chunk out of the main buffer.
for (var i = 4; i < length; i++) {
chunk[i] = data[idx++];
}
var crcStart = idx;
// Read out the CRC value for comparison.
// It's stored as an Int32.
uint8[3] = data[idx++];
uint8[2] = data[idx++];
uint8[1] = data[idx++];
uint8[0] = data[idx++];
var crcActual = int32[0];
var crcExpect = CRC32.buf(chunk);
if (crcExpect !== crcActual) {
throw new Error(
'CRC values for ' + name + ' header do not match, PNG file is likely corrupted'
)
} else {
//console.log("CRCs match! " + crcExpect + " " + crcActual + " " + chunk.length);
}
if (name == "IDAT") {
chunkDataStart = chunkDataStart - 8;
var len = data.length;
// create new array with pHYs chunk inserted
// 4+4+13
var data2 = new Uint8Array(len + 21);
// copy before IEND
for (var i = 0; i < chunkDataStart; i++) {
data2[i] = data[i];
}
// copy IEND to end
for (var i = chunkDataStart; i < len; i++) {
data2[i + 21] = data[i];
}
var phys = new Uint8Array(13);
var i = 0;
// length of pHYs chunk
int32[0] = 9;
data2[chunkDataStart++] = uint8[3];
data2[chunkDataStart++] = uint8[2];
data2[chunkDataStart++] = uint8[1];
data2[chunkDataStart++] = uint8[0];
// pHYs (chunk name)
phys[i++] = data2[chunkDataStart++] = 'p'.charCodeAt(0);
phys[i++] = data2[chunkDataStart++] = 'H'.charCodeAt(0);
phys[i++] = data2[chunkDataStart++] = 'Y'.charCodeAt(0);
phys[i++] = data2[chunkDataStart++] = 's'.charCodeAt(0);
// x
uint32[0] = ppmx;
phys[i++] = data2[chunkDataStart++] = uint8[3];
phys[i++] = data2[chunkDataStart++] = uint8[2];
phys[i++] = data2[chunkDataStart++] = uint8[1];
phys[i++] = data2[chunkDataStart++] = uint8[0];
// y
uint32[0] = ppmy;
phys[i++] = data2[chunkDataStart++] = uint8[3];
phys[i++] = data2[chunkDataStart++] = uint8[2];
phys[i++] = data2[chunkDataStart++] = uint8[1];
phys[i++] = data2[chunkDataStart++] = uint8[0];
// unit = meters
phys[i++] = data2[chunkDataStart++] = 1;
var physCRC = CRC32.buf(phys);
int32[0] = physCRC;
data2[chunkDataStart++] = uint8[3];
data2[chunkDataStart++] = uint8[2];
data2[chunkDataStart++] = uint8[1];
data2[chunkDataStart++] = uint8[0];
return data2;
}
if (name == "pHYs") {
//console.log("pHYs chunk found, rewriting!!!!!!!!!!!!!");
var phys = new Uint8Array(13);
var i = 0;
// pHYs (chunk name)
phys[i++] = 'p'.charCodeAt(0);
phys[i++] = 'H'.charCodeAt(0);
phys[i++] = 'Y'.charCodeAt(0);
phys[i++] = 's'.charCodeAt(0);
// x
uint32[0] = ppmx;
phys[i++] = data[chunkDataStart++] = uint8[3];
phys[i++] = data[chunkDataStart++] = uint8[2];
phys[i++] = data[chunkDataStart++] = uint8[1];
phys[i++] = data[chunkDataStart++] = uint8[0];
// y
uint32[0] = ppmy;
phys[i++] = data[chunkDataStart++] = uint8[3];
phys[i++] = data[chunkDataStart++] = uint8[2];
phys[i++] = data[chunkDataStart++] = uint8[1];
phys[i++] = data[chunkDataStart++] = uint8[0];
// unit = meters
phys[i++] = data[chunkDataStart++] = 1;
var physCRC = CRC32.buf(phys);
int32[0] = physCRC;
data[crcStart++] = uint8[3];
data[crcStart++] = uint8[2];
data[crcStart++] = uint8[1];
data[crcStart++] = uint8[0];
return data;
}
}
throw new Error('.png file ended prematurely: no IEND or pHYs header was found');
}