Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 6e7e7d5

Browse files
committed
[dropper] Maybe probably hopefully finally PickingOnlyBlack bug fix
1 parent 716991f commit 6e7e7d5

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

src/edropper2.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var page = {
3030
canvasContext: null,
3131
canvasBorders: 20,
3232
imageData: null,
33+
resetCanvas: true,
3334

3435
rects: [] as Array<Rect>,
3536

@@ -42,6 +43,7 @@ var page = {
4243
page.screenHeight = window.innerHeight
4344

4445
page.canvas = document.createElement('canvas')
46+
page.resetCanvas = true
4547
page.rects = []
4648
page.screenshoting = false
4749
page.width = Math.round(document.documentElement.scrollWidth)
@@ -252,26 +254,24 @@ var page = {
252254
checkCanvas: function() {
253255
const scale = window.devicePixelRatio
254256

255-
// width and height with borders
256-
// const widthWB = page.width * scale + page.canvasBorders
257-
// const heightWB = page.height * scale + page.canvasBorders
258-
const widthWB = page.width + page.canvasBorders
259-
const heightWB = page.height + page.canvasBorders
260-
261257
// we have to create new canvas element
262-
if (page.canvas.width != widthWB || page.canvas.height != heightWB) {
258+
if (
259+
page.resetCanvas ||
260+
page.canvas.width != page.width ||
261+
page.canvas.height != page.height
262+
) {
263263
page.canvas = document.createElement('canvas')
264-
page.canvas.width = widthWB
265-
page.canvas.height = heightWB
264+
page.canvas.width = page.width
265+
page.canvas.height = page.height
266266

267267
console.log(
268-
`dropper: creating new canvas ${page.canvas.width}x${page.canvas.height}. Pixel Ratio: ${window.devicePixelRatio}`,
268+
`dropper: creating new canvas ${page.canvas.width}x${page.canvas.height}. Pixel Ratio: ${window.devicePixelRatio}. Page dimension: ${page.width}x${page.height}`,
269269
)
270270

271271
page.canvasContext = page.canvas.getContext('2d')
272272
page.canvasContext.scale(1 / scale, 1 / scale)
273-
// page.canvasContext.scale(scale, scale)
274273
page.rects = []
274+
page.resetCanvas = false
275275
}
276276
},
277277
setScreenshoting: function(state) {
@@ -351,11 +351,27 @@ var page = {
351351
var image = document.createElement('img')
352352
image.onload = function() {
353353
console.log(`dropper: got new screenshot ${image.width}x${image.height}`)
354-
// page.screenWidth = image.width
355-
// page.screenHeight = image.height
356-
const rect = new Rect(page.xOffset, page.yOffset, image.width, image.height)
354+
355+
const rect = new Rect(
356+
page.xOffset,
357+
page.yOffset,
358+
Math.round(image.width / window.devicePixelRatio),
359+
Math.round(image.height / window.devicePixelRatio),
360+
)
357361
page.updateRects(rect)
358-
page.canvasContext.drawImage(image, page.xOffset, page.yOffset)
362+
363+
// we changed scale of canvasContext and image data are parsed accroding to this
364+
// but unfortunately not sx,sy dimensions so we need to adjust them
365+
const scale = window.devicePixelRatio
366+
const sx = page.xOffset * scale
367+
const sy = page.yOffset * scale
368+
369+
console.log(
370+
`dropper: drawing image at ${page.xOffset},${page.yOffset} and internally at ${sx},${sy}`,
371+
)
372+
page.canvasContext.drawImage(image, sx, sy)
373+
374+
// get whole canvas data
359375
page.canvasData = page.canvasContext.getImageData(
360376
0,
361377
0,
@@ -384,6 +400,8 @@ var page = {
384400
page.onWindowResize()
385401
}
386402

403+
// FIXME: implement device pixel ration changes same as scrollstop
404+
// with some timeout so it works ok during zoom change i.e.
387405
const mqString = `(resolution: ${window.devicePixelRatio}dppx)`
388406
matchMedia(mqString).addListener(page.onWindowResize)
389407
},

0 commit comments

Comments
 (0)