-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(perPixelTargetFind
): support not-selected
option
#9167
Changes from all commits
67191d6
dc823b8
3925fe0
c23d389
f0cecd7
e5bb4e0
e2fd0c0
969adfb
08aa8a2
cbc0e6b
aa46546
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import { isCollection } from '../util/typeAssertions'; | |
import { invertTransform, transformPoint } from '../util/misc/matrix'; | ||
import { isTransparent } from '../util/misc/isTransparent'; | ||
import type { | ||
PerPixelTargetFind, | ||
TMat2D, | ||
TOriginX, | ||
TOriginY, | ||
|
@@ -167,7 +168,7 @@ export class SelectableCanvas<EventSpec extends CanvasEvents = CanvasEvents> | |
declare containerClass: string; | ||
|
||
// target find config | ||
declare perPixelTargetFind: boolean; | ||
declare perPixelTargetFind: PerPixelTargetFind; | ||
declare targetFindTolerance: number; | ||
declare skipTargetFind: boolean; | ||
|
||
|
@@ -772,25 +773,20 @@ export class SelectableCanvas<EventSpec extends CanvasEvents = CanvasEvents> | |
obj: FabricObject, | ||
globalPointer: Point | ||
): boolean { | ||
if ( | ||
obj && | ||
obj.visible && | ||
obj.evented && | ||
// http://www.geog.ubc.ca/courses/klink/gis.notes/ncgia/u32.html | ||
// http://idav.ucdavis.edu/~okreylos/TAship/Spring2000/PointInPolygon.html | ||
obj.containsPoint(pointer) | ||
) { | ||
if ( | ||
(this.perPixelTargetFind || obj.perPixelTargetFind) && | ||
!(obj as unknown as IText).isEditing | ||
) { | ||
if (!this.isTargetTransparent(obj, globalPointer.x, globalPointer.y)) { | ||
return true; | ||
} | ||
} else { | ||
return true; | ||
} | ||
if (obj && obj.visible && obj.evented && obj.containsPoint(pointer)) { | ||
const shouldPerformPixelFind = | ||
!(obj as unknown as IText).isEditing && | ||
(this.perPixelTargetFind === true || | ||
obj.perPixelTargetFind === true || | ||
((this.perPixelTargetFind === 'not-selected' || | ||
obj.perPixelTargetFind === 'not-selected') && | ||
Comment on lines
+779
to
+782
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure about combinations between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should collect the current rules around the code. |
||
!this.getActiveObjects().includes(obj))); | ||
|
||
return shouldPerformPixelFind | ||
? !this.isTargetTransparent(obj, globalPointer.x, globalPointer.y) | ||
: true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Point } from '../../Point'; | ||
import type { IText } from '../../shapes/IText/IText'; | ||
import { FabricObject } from '../../shapes/Object/FabricObject'; | ||
import { Canvas } from '../Canvas'; | ||
|
||
describe('perPixelTargetFind', () => { | ||
let canvas: Canvas; | ||
let object: FabricObject; | ||
let isTargetTransparent: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
canvas = new Canvas(null); | ||
isTargetTransparent = jest | ||
.spyOn(canvas, 'isTargetTransparent') | ||
.mockReturnValue(true); | ||
object = new FabricObject(); | ||
jest.spyOn(object, 'containsPoint').mockReturnValue(true); | ||
}); | ||
|
||
afterEach(() => { | ||
return canvas.dispose(); | ||
}); | ||
|
||
const checkTarget = () => | ||
canvas._checkTarget(new Point(), object, new Point()); | ||
|
||
test('perPixelTargetFind === false', () => { | ||
expect(checkTarget()).toBe(true); | ||
expect(isTargetTransparent).not.toBeCalled(); | ||
}); | ||
|
||
test('perPixelTargetFind === true', () => { | ||
object.perPixelTargetFind = true; | ||
expect(checkTarget()).toBe(false); | ||
expect(isTargetTransparent).toBeCalled(); | ||
}); | ||
|
||
test('perPixelTargetFind === true, object is editing', () => { | ||
object.perPixelTargetFind = true; | ||
(object as IText).isEditing = true; | ||
expect(checkTarget()).toBe(true); | ||
expect(isTargetTransparent).not.toBeCalled(); | ||
}); | ||
|
||
test('perPixelTargetFind === "not-selected", object is not selected', () => { | ||
object.perPixelTargetFind = 'not-selected'; | ||
expect(checkTarget()).toBe(false); | ||
expect(isTargetTransparent).toBeCalled(); | ||
}); | ||
|
||
test('perPixelTargetFind === "not-selected", object is selected', () => { | ||
object.perPixelTargetFind = 'not-selected'; | ||
canvas.setActiveObject(object); | ||
expect(checkTarget()).toBe(true); | ||
expect(isTargetTransparent).not.toBeCalled(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should preserve this comment maybe over the containsPoint function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
both are dead, and also the impl has moved forward more than a decade