@@ -7,8 +7,10 @@ type Tile = {
77 highlight ?: boolean ;
88} ;
99type BoardTile = Tile & {
10- clientX : number ;
11- clientY : number ;
10+ clientLeft : number ;
11+ clientTop : number ;
12+ clientRight : number ;
13+ clientBottom : number ;
1214 x : number ;
1315 y : number ;
1416 z : number ;
@@ -22,7 +24,7 @@ const boardLength = 8;
2224const tileWidth = 64 ;
2325const tileHeight = 96 ;
2426const faceX = 4 ;
25- const faceY = 4 ;
27+ const faceY = - 4 ;
2628
2729const shuffle = < T > ( xs : T [ ] , k = 0 ) => {
2830 const alreadyShuffled = new Set < number > ( ) ;
@@ -61,7 +63,11 @@ export const InitGame = async (ctx: CanvasRenderingContext2D) => {
6163 const sp = [ ...space ] [ spaceIdx ] ;
6264 if ( ! sp ) break ;
6365 const s = obj ( sp ) ;
64- board . push ( { asset, x : s . x , y : s . y , z : s . z , clientX : 0 , clientY : 0 } ) ;
66+ const client = {
67+ ...{ clientLeft : 0 , clientTop : 0 } ,
68+ ...{ clientRight : 0 , clientBottom : 0 } ,
69+ } ;
70+ board . push ( { asset, x : s . x , y : s . y , z : s . z , ...client } ) ;
6571 occupied . add ( sp ) ;
6672 if ( s . x > s . z * 2 && s . y > s . z * 2 ) {
6773 if ( has ( s , - 2 , 0 ) && has ( s , - 2 , - 2 ) && has ( s , 0 , - 2 ) )
@@ -89,14 +95,14 @@ export const InitGame = async (ctx: CanvasRenderingContext2D) => {
8995 }
9096 }
9197
92- ctx . canvas . addEventListener ( 'mousemove' , e => {
93- mouse . x = e . clientX ;
94- mouse . y = e . clientY ;
98+ ctx . canvas . addEventListener ( 'pointermove' , e => {
99+ const rect = ctx . canvas . getBoundingClientRect ( ) ;
100+ mouse . x = ( e . clientX - rect . left ) * ( ctx . canvas . width / rect . width ) ;
101+ mouse . y = ( e . clientY - rect . top ) * ( ctx . canvas . height / rect . height ) ;
95102 } ) ;
96- ctx . canvas . addEventListener ( 'mousedown ' , ( ) => {
103+ ctx . canvas . addEventListener ( 'pointerdown ' , ( ) => {
97104 mouse . clicked = true ;
98105 } ) ;
99-
100106 CalcClickable ( ) ;
101107} ;
102108
@@ -111,63 +117,68 @@ const RenderTile = (
111117 ctx . lineWidth = 1 ;
112118 ctx . strokeStyle = '#000' ;
113119 ctx . translate ( ( x / 2 ) * tileWidth , ( y / 2 ) * tileHeight ) ;
114- ctx . translate ( z * faceX , z * - faceY ) ;
120+ ctx . translate ( z * faceX , z * faceY ) ;
115121
116122 const highlight = tile . highlight && tile . clickable ;
117123 //Left side
118124 ctx . fillStyle = highlight ? '#0a0' : '#eee' ;
119125 ctx . beginPath ( ) ;
120126 ctx . moveTo ( 0 , 0 ) ;
121127 ctx . lineTo ( 0 , tileHeight ) ;
122- ctx . lineTo ( faceX , tileHeight - faceY ) ;
123- ctx . lineTo ( faceX , - faceY ) ;
128+ ctx . lineTo ( faceX , tileHeight + faceY ) ;
129+ ctx . lineTo ( faceX , faceY ) ;
124130 ctx . closePath ( ) ;
125131 ctx . fill ( ) ;
126132 ctx . stroke ( ) ;
127133 //Front face
128134 ctx . fillStyle = highlight ? '#080' : '#ccc' ;
129135 ctx . beginPath ( ) ;
130136 ctx . moveTo ( 0 , tileHeight ) ;
131- ctx . lineTo ( faceX , tileHeight - faceY ) ;
132- ctx . lineTo ( tileWidth + faceX , tileHeight - faceY ) ;
137+ ctx . lineTo ( faceX , tileHeight + faceY ) ;
138+ ctx . lineTo ( tileWidth + faceX , tileHeight + faceY ) ;
133139 ctx . lineTo ( tileWidth , tileHeight ) ;
134140 ctx . closePath ( ) ;
135141 ctx . fill ( ) ;
136142 ctx . stroke ( ) ;
137143
138144 //Background
139145 ctx . fillStyle = highlight ? '#aaffbb' : '#fffadb' ;
140- ctx . fillRect ( faceX , - faceY , tileWidth , tileHeight ) ;
146+ ctx . fillRect ( faceX , faceY , tileWidth , tileHeight ) ;
141147 //Border
142148 ctx . strokeStyle = '#000000' ;
143149 ctx . lineWidth = 1 ;
144- ctx . strokeRect ( faceX , - faceY , tileWidth , tileHeight ) ;
150+ ctx . strokeRect ( faceX , faceY , tileWidth , tileHeight ) ;
145151 //Tile image
146152 ctx . drawImage (
147153 tile . asset ,
148154 faceX + 2 ,
149- - faceY + 2 ,
155+ faceY + 2 ,
150156 tileWidth - faceX ,
151- tileHeight - faceY ,
157+ tileHeight + faceY ,
152158 ) ;
153- const point = new DOMPoint ( 0 , 0 ) . matrixTransform ( ctx . getTransform ( ) ) ;
154- if ( 'clientX' in tile ) {
155- tile . clientX = point . x ;
156- tile . clientY = point . y ;
159+ const t = ( x : number , y : number ) =>
160+ new DOMPoint ( x , y ) . matrixTransform ( ctx . getTransform ( ) ) ;
161+ const topLeft = t ( faceX , faceY ) ;
162+ const bottomRight = t ( tileWidth + faceX , tileHeight ) ;
163+ if ( 'clientLeft' in tile ) {
164+ tile . clientLeft = topLeft . x ;
165+ tile . clientTop = topLeft . y ;
166+ tile . clientRight = bottomRight . x ;
167+ tile . clientBottom = bottomRight . y ;
157168
158169 // //Euclidean distance brightening effect
159170 // //by darkening farther tiles
160171 // const distance = hypot(
161172 // mouse.x - (tile.clientX + (tileWidth - faceX) / 2),
162- // mouse.y - (tile.clientY + (tileHeight - faceY) / 2),
173+ // mouse.y - (tile.clientY + (tileHeight + faceY) / 2),
163174 // );
164175 // const maxDistance = 300;
165176 // const clamped = min(distance, maxDistance);
166177 // const darkness = floor((clamped / maxDistance) * 64);
167178 // ctx.fillStyle = `rgba(0,0,0,${darkness / 255})`;
168179 const maxZ = 4 ;
169180 ctx . fillStyle = `rgba(0,0,0,${ ( maxZ - tile . z ) / ( maxZ * 4 ) } )` ;
170- ctx . fillRect ( faceX , - faceY , tileWidth , tileHeight ) ;
181+ ctx . fillRect ( faceX , faceY , tileWidth , tileHeight ) ;
171182 }
172183
173184 ctx . restore ( ) ;
@@ -261,13 +272,11 @@ export const Render = (ctx: CanvasRenderingContext2D) => {
261272 . reduce (
262273 ( under , tile ) => {
263274 tile . highlight = false ;
264- const clientX = tile . clientX ?? 0 ;
265- const clientY = tile . clientY ?? 0 ;
266275 const isUnder =
267- mouse . x >= clientX &&
268- mouse . x <= clientX + ( tileWidth + faceX ) * scale &&
269- mouse . y >= clientY - faceY * scale &&
270- mouse . y <= clientY + tileHeight * scale ;
276+ mouse . x >= tile . clientLeft &&
277+ mouse . x <= tile . clientRight &&
278+ mouse . y >= tile . clientTop &&
279+ mouse . y <= tile . clientBottom ;
271280 if ( ! isUnder ) return under ;
272281 if ( under ) return tile . z > under . z ? tile : under ;
273282 return tile ;
0 commit comments