@@ -2,7 +2,7 @@ import React, { useEffect, useRef } from 'react';
22import { environment } from '#store/environment' ;
33import { observer } from 'mobx-react' ;
44import { exportSprite } from '#formats/image' ;
5- import { rotateImageData } from '#util/rotsprite' ;
5+ import { rotateImageData , getRotateDiagonal } from '#util/rotsprite' ;
66import { Input , Slider , Item , Button , Modal } from '#ui' ;
77import { mappingState } from './state' ;
88
@@ -11,14 +11,58 @@ import { importState } from '../import/state';
1111function rotateCurrentSprite ( canvas , angle ) {
1212 const spriteCanv = exportSprite ( environment . currentSprite ) ;
1313 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 ) ;
1735
1836 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+ }
2266}
2367
2468export const Rotate = observer ( ( ) => {
0 commit comments