@@ -2,7 +2,7 @@ import React, { useEffect, useRef } from 'react';
2
2
import { environment } from '#store/environment' ;
3
3
import { observer } from 'mobx-react' ;
4
4
import { exportSprite } from '#formats/image' ;
5
- import { rotateImageData } from '#util/rotsprite' ;
5
+ import { rotateImageData , getRotateDiagonal } from '#util/rotsprite' ;
6
6
import { Input , Slider , Item , Button , Modal } from '#ui' ;
7
7
import { mappingState } from './state' ;
8
8
@@ -11,14 +11,58 @@ import { importState } from '../import/state';
11
11
function rotateCurrentSprite ( canvas , angle ) {
12
12
const spriteCanv = exportSprite ( environment . currentSprite ) ;
13
13
const spriteCtx = spriteCanv . getContext ( '2d' ) ;
14
- const { width, height } = spriteCanv ;
15
- const imageData = spriteCtx . getImageData ( 0 , 0 , width , height ) ;
16
- const rotatedData = rotateImageData ( imageData , angle , width , height ) ;
14
+ // const { width, height } = spriteCanv;
15
+ // const imageData = spriteCtx.getImageData(0, 0, width, height);
16
+ // const rotatedData = rotateImageData(imageData, angle, width, height);
17
+
18
+ // const ctx = canvas.getContext('2d');
19
+ // canvas.width = rotatedData.width;
20
+ // canvas.height = rotatedData.height;
21
+ // ctx.putImageData(rotatedData, 0, 0);
22
+
23
+
24
+ // add padding
25
+ const { diagonal, xMargin, yMargin } = getRotateDiagonal ( spriteCanv . width , spriteCanv . height ) ;
26
+ const width = diagonal ;
27
+ const height = diagonal ;
28
+
29
+ const copy = spriteCtx . getImageData ( 0 , 0 , spriteCanv . width , spriteCanv . height ) ;
30
+
31
+ spriteCanv . width = diagonal ;
32
+ spriteCanv . height = diagonal ;
33
+
34
+ spriteCtx . putImageData ( copy , xMargin , yMargin ) ;
17
35
18
36
const ctx = canvas . getContext ( '2d' ) ;
19
- canvas . width = rotatedData . width ;
20
- canvas . height = rotatedData . height ;
21
- ctx . putImageData ( rotatedData , 0 , 0 ) ;
37
+ canvas . width = width ;
38
+ canvas . height = height ;
39
+
40
+ // rotate
41
+
42
+ const theta = angle * Math . PI / 180 ;
43
+ const alpha = - Math . tan ( theta / 2 ) ;
44
+ const beta = Math . sin ( theta ) ;
45
+
46
+
47
+
48
+ for ( let y = 0 ; y < height ; ++ y ) {
49
+ const shear = Math . round ( ( y - height / 2 ) * alpha ) ;
50
+ ctx . drawImage ( spriteCanv , 0 , y , width , 1 , shear , y , width , 1 ) ;
51
+ }
52
+ spriteCtx . clearRect ( 0 , 0 , width , height ) ;
53
+ spriteCtx . drawImage ( canvas , 0 , 0 ) ;
54
+ ctx . clearRect ( 0 , 0 , width , height ) ;
55
+ for ( let x = 0 ; x < width ; ++ x ) {
56
+ const shear = Math . round ( ( x - width / 2 ) * beta ) ;
57
+ ctx . drawImage ( spriteCanv , x , 0 , 1 , height , x , shear , 1 , height ) ;
58
+ }
59
+ spriteCtx . clearRect ( 0 , 0 , width , height ) ;
60
+ spriteCtx . drawImage ( canvas , 0 , 0 ) ;
61
+ ctx . clearRect ( 0 , 0 , width , height ) ;
62
+ for ( let y = 0 ; y < height ; ++ y ) {
63
+ const shear = Math . round ( ( y - height / 2 ) * alpha ) ;
64
+ ctx . drawImage ( spriteCanv , 0 , y , width , 1 , shear , y , width , 1 ) ;
65
+ }
22
66
}
23
67
24
68
export const Rotate = observer ( ( ) => {
0 commit comments