-
Notifications
You must be signed in to change notification settings - Fork 2
/
affintrans.js
134 lines (128 loc) · 3.67 KB
/
affintrans.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
"use strict";
/*
* 2017/04/21- (c) [email protected]
*/
document.addEventListener("DOMContentLoaded", function(event) {
main();
});
function main() {
// console.debug("main");
var srcCanvas = document.getElementById("srcCanvas");
var dstCanvas = document.getElementById("dstCanvas");
var srcImage = new Image(srcCanvas.width, srcCanvas.height);
//
var affinMatrixTable = document.getElementById("affinMatrixTable");
var affin = document.getElementById("affinSelect").value;
var affinMatrix = affin2Matrix(affin, srcCanvas);
var affinWindow = 3;
var params = {};
//
dropFunction(document, function(dataURL) {
srcImage = new Image();
srcImage.onload = function() {
let maxWidthHeight = params["maxWidthHeightRange"];
drawSrcImage(srcImage, srcCanvas, maxWidthHeight);
drawAffinTransform(srcCanvas, dstCanvas, affinMatrix, params, true);
}
srcImage.src = dataURL;
}, "DataURL");
//
bindFunction({"maxWidthHeightRange":"maxWidthHeightText"},
function(target, rel) {
let maxWidthHeight = params["maxWidthHeightRange"];
drawSrcImage(srcImage, srcCanvas, maxWidthHeight);
drawAffinTransform(srcCanvas, dstCanvas, affinMatrix, params, rel);
}, params);
bindFunction({"affinSelect":null, "outfillSelect":null},
function(target, rel) {
let affin = params["affinSelect"];
affinMatrix = affin2Matrix(affin, srcCanvas);
drawAffinTransform(srcCanvas, dstCanvas, affinMatrix, params, rel);
setTableValues("affinMatrixTable", affinMatrix);
}, params);
//
bindTableFunction("affinMatrixTable", function(table, values, width) {
affinMatrix = values;
drawAffinTransform(srcCanvas, dstCanvas, affinMatrix, params, true);
}, affinMatrix, affinWindow);
//
affin = params["affinSelect"];
affinMatrix = affin2Matrix(affin, srcCanvas);
}
function affin2Matrix(affin, canvas) {
var mat = null;
switch (affin) {
case "ident":
mat = [1, 0, 0,
0, 1, 0,
0, 0, 1];
break;
case "scale2":
mat = [2, 0, 0,
0, 2, 0,
0, 0, 1];
break;
case "scale0.5":
mat = [0.5, 0, 0,
0, 0.5, 0,
0, 0, 1];
break;
case "skew0.2x":
mat = [1, 0.2, 0,
0, 1, 0,
0, 0, 1];
break;
case "skew0.2y":
mat = [1, 0, 0,
0.2, 1, 0,
0, 0, 1];
break;
case "flop":
mat = [-1, 0, canvas.width,
0, 1, 0,
0, 0, 1];
break;
case "flip":
mat = [1, 0, 0,
0, -1, canvas.height,
0, 0, 1];
break;
case "rotate45":
var theta = Math.PI / 4;
mat = [Math.cos(theta), -Math.sin(theta), 0,
Math.sin(theta), Math.cos(theta), 0,
0, 0, 1];
mat[2] = - mat[1] * canvas.height; // minX
break;
case "rotate90":
var theta = Math.PI / 2;
mat = [Math.cos(theta), -Math.sin(theta), canvas.height,
Math.sin(theta), Math.cos(theta), 0,
0, 0, 1];
break;
case "rotate180":
var theta = Math.PI / 2 * 2;
mat = [Math.cos(theta), -Math.sin(theta), canvas.width,
Math.sin(theta), Math.cos(theta), canvas.height,
0, 0, 1];
break;
case "rotate270":
var theta = Math.PI / 2 * 3;
mat = [Math.cos(theta), -Math.sin(theta), 0,
Math.sin(theta), Math.cos(theta), canvas.width,
0, 0, 1];
break;
default:
console.error("Invalid affin:"+affin);
break;
}
return mat;
};
var worker = new workerProcess("worker/affintrans.js");
function drawAffinTransform(srcCanvas, dstCanvas, affinMatrix, params, sync) {
var params_w = {
affinMatrix: affinMatrix,
outfill : outfillStyleNumber(params["outfillSelect"]),
};
worker.process(srcCanvas, dstCanvas, params_w, sync);
}