Skip to content

Commit 841ed33

Browse files
authored
fix: select an asset without prior hover (#23)
1 parent d1ac806 commit 841ed33

File tree

10 files changed

+102
-8
lines changed

10 files changed

+102
-8
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// npx playwright test asset-basic-transform.spec.ts --debug
2+
3+
import { test, expect } from '@playwright/test'
4+
import path from 'path'
5+
import { fileURLToPath } from 'url'
6+
7+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
8+
const PAGE_WIDTH = 1000
9+
const PAGE_HEIGHT = 700
10+
const center = { x: PAGE_WIDTH / 2, y: PAGE_HEIGHT / 2 }
11+
12+
test('asset performs basic transformations', async ({ page }, testinfo) => {
13+
if (process.env.CI) {
14+
test.skip()
15+
return
16+
}
17+
18+
testinfo.snapshotSuffix = '' // by default is `process.platform`
19+
20+
// and it produces different screenshot name base on operating system
21+
// while we want to make app consistent on all operating systems
22+
23+
// To finally check if WebGPU is supported
24+
// await page.goto('https://webgpureport.org/');
25+
// await expect(page).toHaveScreenshot('webgpu-report.png');
26+
27+
await page.setViewportSize({ width: PAGE_WIDTH, height: PAGE_HEIGHT })
28+
await page.goto('/')
29+
await page.waitForLoadState('networkidle')
30+
await page.evaluate(() =>
31+
window.addEventListener('mousemove', (e) => console.log(e.clientX, e.clientY))
32+
) // to display cursor position during debugging
33+
// helps to copy position here
34+
35+
/** =========PNG IMAGE UPLOAD============ */
36+
const fileInput = page.locator('input[type="file"]')
37+
const testImagePath = path.join(__dirname, '../image-sample.png')
38+
await fileInput.setInputFiles(testImagePath)
39+
40+
const canvas = page.locator('canvas')
41+
42+
// select asset
43+
await page.mouse.move(center.x, center.y)
44+
await page.mouse.down()
45+
await page.mouse.up()
46+
47+
// move
48+
await page.mouse.down()
49+
await page.mouse.move(300, 300)
50+
await page.mouse.up()
51+
await expect(canvas).toHaveScreenshot('move-image.png')
52+
53+
// rotate
54+
await page.mouse.down()
55+
await page.mouse.move(573, 533)
56+
await page.mouse.up()
57+
await expect(canvas).toHaveScreenshot('rotate-image.png')
58+
59+
// scale with top middle handler
60+
await page.mouse.down()
61+
await page.mouse.move(320, 264)
62+
await page.mouse.up()
63+
await expect(canvas).toHaveScreenshot('use-top-scale-ui.png')
64+
65+
// TODO: do rotation test
66+
67+
// scale with top left andler
68+
await page.mouse.down()
69+
await page.mouse.move(950, 400)
70+
await page.mouse.up()
71+
await expect(canvas).toHaveScreenshot('use-top-left-scale-ui.png')
72+
})
316 KB
Loading
310 KB
Loading
310 KB
Loading
316 KB
Loading
316 KB
Loading
314 KB
Loading

src/WebGPU/pick.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ export default class PickManager {
109109
await this.pickBuffer.mapAsync(GPUMapMode.READ, 0, 4 * NUM_PIXELS)
110110
const [id] = new Uint32Array(this.pickBuffer.getMappedRange(0, 4 * NUM_PIXELS))
111111
on_update_pick(id)
112+
113+
if (pointer.downCallback) {
114+
// to me 100% precise we should also check timestamp if pickign started after the click happened
115+
// but not sure if it's possible to expose this issue
116+
pointer.downCallback()
117+
pointer.downCallback = null // reset callback after use
118+
}
119+
112120
this.pickBuffer.unmap()
113121
// eslint-disable-next-line @typescript-eslint/no-unused-vars
114122
} catch (err) {

src/WebGPU/pointer.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { on_pointer_move, on_pointer_down, on_pointer_up } from '../logic/index.zig'
22

3-
export const pointer = { x: 0, y: 0 }
3+
export const pointer = {
4+
x: 0,
5+
y: 0,
6+
downCallback: null as VoidFunction | null,
7+
}
48

59
export default function initMouseController(canvas: HTMLCanvasElement) {
610
pointer.x = 0
@@ -19,8 +23,11 @@ export default function initMouseController(canvas: HTMLCanvasElement) {
1923
on_pointer_move(pointer.x, canvas.height - pointer.y)
2024
})
2125

22-
canvas.addEventListener('mousedown', () => {
23-
on_pointer_down(pointer.x, canvas.height - pointer.y)
26+
canvas.addEventListener('mousedown', (e) => {
27+
updatePointer(e)
28+
pointer.downCallback = () => {
29+
on_pointer_down(pointer.x, canvas.height - pointer.y)
30+
}
2431
})
2532

2633
canvas.addEventListener('mouseup', () => {

src/run.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import getCanvasMatrix from 'getCanvasMatrix'
1010
import PickManager from 'WebGPU/pick'
1111
import { canvas_render, picks_render, connect_web_gpu_programs } from 'logic/index.zig'
1212
import { TextureSource } from '.'
13+
import { pointer } from 'WebGPU/pointer'
1314

1415
export const transformMatrix = new Float32Array()
1516
export const MAP_BACKGROUND_SCALE = 1000
@@ -43,6 +44,7 @@ export default function runCreator(
4344
})
4445

4546
let rafId = 0
47+
const lastPickPointer: Point = { x: 0, y: 0 }
4648

4749
function draw(now: DOMHighResTimeStamp) {
4850
const encoder = device.createCommandEncoder()
@@ -52,16 +54,21 @@ export default function runCreator(
5254
canvas_render()
5355
canvasPass.end()
5456

55-
pickMatrix = pickManager.createMatrix(canvas, canvasMatrix)
56-
const pick = pickManager.startPicking(encoder)
57-
pickPass = pick.pass
58-
picks_render()
59-
pick.end()
57+
if (lastPickPointer.x !== pointer.x || lastPickPointer.y !== pointer.y) {
58+
lastPickPointer.x = pointer.x
59+
lastPickPointer.y = pointer.y
60+
pickMatrix = pickManager.createMatrix(canvas, canvasMatrix)
61+
const pick = pickManager.startPicking(encoder)
62+
pickPass = pick.pass
63+
picks_render()
64+
pick.end()
65+
}
6066

6167
const commandBuffer = encoder.finish()
6268
device.queue.submit([commandBuffer])
6369

6470
pickManager.asyncPick()
71+
6572
rafId = requestAnimationFrame(draw)
6673
}
6774

0 commit comments

Comments
 (0)