-
Notifications
You must be signed in to change notification settings - Fork 0
/
sacle-rotate.html
148 lines (131 loc) · 3.18 KB
/
sacle-rotate.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
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="main">
<canvas id="canvas" width="600" height="400"></canvas>
</div>
<script>
var object = function (x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
};
object.prototype.move = function (dx, dy) {
dx = dx || 0;
dy = dy || 0;
this.x += dx;
this.y += dy;
return this;
};
object.prototype.get = function (attr) {
return this[attr];
};
object.prototype.set = function (key, val) {
if(this[key]){
this[key] = val;
return this;
}
else {
return;
}
};
var sprite = function (x, y, w, h) {
object.call(this, x, y, w, h);
this.imgurl = 'baidu.com'
};
sprite.prototype = new object();
sprite.prototype.showUrl = function () {
// console.log(this.imgurl);
}
var sprite1 = new sprite(0, 0, 50, 50);
sprite1.move(100, 100);
// console.log(sprite1.get('imgurl'));
// console.dir(sprite1);
sprite1.showUrl();
</script>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = ctx.stokeStyle = '#ccc';
ctx.fillRect(0, 0, canvas.width, canvas.height);
/*function drawRect (x, y, w, h) {
ctx.save();
ctx.beginPath();
ctx.fillStyle = '#000';
ctx.translate(x + w / 2, y + h / 2);
// ctx.rotate(Math.PI / 3);
ctx.rect(-w / 2, -h / 2, w, h);
ctx.translate(-x - w / 2, -y - h / 2);
ctx.closePath();
ctx.fill();
ctx.restore();
}*/
// ctx.save();
// drawRect(150, 100, 300, 200);
/*获取sacle之后的坐标位置*/
// 二维变换 xPos, yPos为缩放的中心点
/*function sacleMatrix(point, xSacle, yScale, xPos, yPos) {
var m = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 1]
];
m[0][0] = xSacle;
m[1][1] = yScale;
m[2][0] = -(xSacle - 1) * xPos;
m[2][1] = -(yScale - 1) * yPos;
var result = Matrix(point, m);
return result;
};*/
/*获取旋转之后的坐标位置*/
// centerPoin为围绕改点旋转
function rotationMatrix(point, centerPoint, angle) {
console.log(point[0], point[1]);
ctx.fillStyle = '#0f0';
ctx.fillRect(point[0], point[0], 3, 3);
var m = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 1]
];
var cos = Math.cos,
sin = Math.sin,
angle = angle / 180 * Math.PI;
m[0][0] = cos(angle);
m[0][1] = sin(angle);
m[1][0] = -sin(angle);
m[1][1] = cos(angle);
var relativePoint = [], result = [];
for (var i = 0; i < point.length; i++) {
relativePoint.push(point[i] - centerPoint[i]);
}
var tmp = Matrix(relativePoint, m);
for (var i = 0; i < point.length; i++) {
result.push(tmp[i] + centerPoint[i]);
}
console.log(result[0], result[0]);
ctx.fillStyle = '#f00';
ctx.fillRect(result[0], result[0], 3, 3);
return result;
}
function Matrix(arr, matrix) {
var result = [];
for (var i = 0, len = matrix.length; i < len; i++) {
var sum = 0;
for (var j = 0; j < matrix[i].length; j++) {
sum += arr[j] * matrix[j][i];
}
result.push(sum);
}
return result;
}
var arr = [2, 3, 1];
var point = [100, 100, 1];
var centerPoint = [300, 200, 1];
rotationMatrix(point, centerPoint, 0);
</script>
</body>
</html>