-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathiframework-utils.js
112 lines (100 loc) · 2.98 KB
/
iframework-utils.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
$(function () {
Iframework.util = {
// From YUI3 via http://stackoverflow.com/a/7390555/592125
types: {
undefined: 'undefined',
number: 'number',
boolean: 'boolean',
string: 'string',
'[object Function]': 'function',
'[object RegExp]': 'regexp',
'[object Array]': 'array',
'[object Date]': 'date',
'[object Error]': 'error',
'[object HTMLCanvasElement]': 'HTMLCanvasElement',
'[object ImageData]': 'ImageData',
},
type: function (o) {
return (
this.types[typeof o] ||
this.types[Object.prototype.toString.call(o)] ||
(o ? 'object' : 'null')
);
},
imageTypes: ['png', 'gif', 'jpg', 'jpeg', 'webp'],
isImageURL: function (url) {
var fileTypeSplit = url.split('.');
if (fileTypeSplit.length > 1) {
var fileType = fileTypeSplit[fileTypeSplit.length - 1];
return this.imageTypes.indexOf(fileType) > -1;
}
return false;
},
imageDrop: function (event, ui) {
// Used in image.js and variable-animation.js
// TODO only drop to top
// Don't deal with dropped file
if (!ui) {
return false;
}
// Don't also drop on graph
event.stopPropagation();
var self = event.data.self;
var type = ui.helper.data('meemoo-drag-type');
if (!type || type !== 'canvas') {
return false;
}
var inputName = event.data.inputName;
if (!inputName) {
return false;
}
var canvas;
var url = ui.helper.data('meemoo-image-url');
if (url) {
// Load big image instead of thumbnail
var img = new Image();
img.crossOrigin = 'anonymous';
img.onload = function () {
canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0);
// Hit own input with image
self.receive(inputName, canvas);
};
img.src = url;
} else {
canvas = ui.helper.data('meemoo-drag-canvas');
if (!canvas) {
return false;
}
// Hit own input with image
self.receive(inputName, canvas);
}
},
fitAndCopy: function (source, target) {
// source and target 2d canvases
var w = target.width;
var h = target.height;
var ratio = w / h;
var inWidth = source.width;
var inHeight = source.height;
var inRatio = inWidth / inHeight;
var sx, sy, sw, sh;
if (ratio >= inRatio) {
sw = inWidth;
sh = Math.floor(inWidth / ratio);
sx = 0;
sy = Math.floor((inHeight - sh) / 2);
} else {
sw = Math.floor(inHeight * ratio);
sh = inHeight;
sx = Math.floor((inWidth - sw) / 2);
sy = 0;
}
var context = target.getContext('2d');
context.drawImage(source, sx, sy, sw, sh, 0, 0, w, h);
},
};
});