Skip to content
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

patch(TS): canvas patch init 2 #8520

Merged
merged 40 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7c83395
broken progress
asturur Dec 12, 2022
fdf9245
save work
asturur Dec 12, 2022
aa0b140
save progress work
asturur Dec 17, 2022
6c02e74
may i have broke stuff
asturur Dec 17, 2022
84541aa
what did i do
asturur Dec 17, 2022
135ef2a
something killed fabric.config
asturur Dec 18, 2022
54e6351
stupid vscode you killed a full evening
asturur Dec 18, 2022
5ba11af
test passing
asturur Dec 18, 2022
47d717b
minimal changes
asturur Dec 18, 2022
305d6e8
clean up canvas
asturur Dec 18, 2022
1c577f3
patch canvas init logic
ShaMan123 Dec 18, 2022
286967c
fix(): apply canvas style before saving `_originalCanvasStyle`
ShaMan123 Dec 18, 2022
efcf9f1
cleanup redundant call
ShaMan123 Dec 18, 2022
644f94f
Revert "fix(): apply canvas style before saving `_originalCanvasStyle`"
ShaMan123 Dec 18, 2022
c31e5d7
rm redundant objects init
ShaMan123 Dec 19, 2022
281b831
mv
ShaMan123 Dec 19, 2022
9818c73
rm render bound methods
ShaMan123 Dec 19, 2022
143c4cc
test
ShaMan123 Dec 19, 2022
a9c7c7a
Update canvas.js
ShaMan123 Dec 19, 2022
fe21142
Merge branch 'master' into ts-canvas-patch-init
ShaMan123 Dec 29, 2022
4971d73
restore
ShaMan123 Dec 29, 2022
2abdf9a
dump _initEventListeners
ShaMan123 Dec 29, 2022
9ca6002
dump _bindEvents
ShaMan123 Dec 29, 2022
f5e96ca
Update CHANGELOG.md
ShaMan123 Dec 29, 2022
cba9325
fix(): regression unwanted init retina
ShaMan123 Jan 3, 2023
6e1ddc1
Merge branch 'master' into ts-canvas-patch-init
ShaMan123 Jan 3, 2023
359606c
Update CHANGELOG.md
ShaMan123 Jan 3, 2023
65afac6
fix(): set elements size on init
ShaMan123 Jan 3, 2023
b27ac06
forgotten
ShaMan123 Jan 3, 2023
fadd45f
rerfactor(): rm initSize => setDimensions
ShaMan123 Jan 3, 2023
95cc420
fix
ShaMan123 Jan 3, 2023
a1e89a7
cleanup + protected
ShaMan123 Jan 3, 2023
1d848ee
Update canvas.js
ShaMan123 Jan 4, 2023
15c8bf7
Merge branch 'master' into ts-canvas-patch-init
ShaMan123 Jan 6, 2023
a3caeac
Merge branch 'master' into ts-canvas-patch-init
asturur Jan 14, 2023
7c4910d
fix(): bad merge
ShaMan123 Jan 15, 2023
d8557f3
first commit
asturur Jan 15, 2023
e6148fc
changes to setDimensions
asturur Jan 15, 2023
e1ffe0c
mantain terser bind code
asturur Jan 15, 2023
9d19204
of course prettier
asturur Jan 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- chore(TS): refactor canvas init, fix `_initRetinaScaling` regression #8520
- fix(): regression of canvas migration with pointer and sendPointToPlane [#8563](https://github.com/fabricjs/fabric.js/pull/8563)
- chore(TS): Use exports from files to build fabricJS, get rid of HEADER.js [#8549](https://github.com/fabricjs/fabric.js/pull/8549)
- chore(): rm `fabric.filterBackend` => `getFilterBackend` [#8487](https://github.com/fabricjs/fabric.js/pull/8487)
Expand Down
51 changes: 9 additions & 42 deletions src/canvas/canvas.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,34 +497,16 @@ export class SelectableCanvas<
_activeObject?: FabricObject;
_hasITextHandlers?: boolean;
_iTextInstances: (IText | Textbox)[];
/**
* Constructor
* @param {HTMLCanvasElement | String} el canvas element to initialize instance on
* @param {Object} [options] Options object
* @return {Object} thisArg
*/
constructor(el: string | HTMLCanvasElement, options = {}) {
super(el, options);
}

_init(el: string | HTMLCanvasElement, options = {}) {
this.renderAndResetBound = this.renderAndReset.bind(this);
this.requestRenderAllBound = this.requestRenderAll.bind(this);
this._initStatic(el, options);
protected initElements(el: string | HTMLCanvasElement) {
super.initElements(el);
this._applyCanvasStyle(this.lowerCanvasEl);
this._initWrapperElement();
this._createUpperCanvas();
// @ts-ignore
this._initEventListeners();
this._initRetinaScaling();
this.calcOffset();
this._createCacheCanvas();
}

/**
* @private
*/
_initRetinaScaling() {
protected _initRetinaScaling() {
super._initRetinaScaling();
this.__initRetinaScaling(this.upperCanvasEl, this.contextTop);
ShaMan123 marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -1233,7 +1215,7 @@ export class SelectableCanvas<
* @private
* @throws {CANVAS_INIT_ERROR} If canvas can not be initialized
*/
_createUpperCanvas() {
protected _createUpperCanvas() {
const lowerCanvasEl = this.lowerCanvasEl;

// if there is no upperCanvas (most common case) we create one.
Expand All @@ -1252,30 +1234,15 @@ export class SelectableCanvas<
upperCanvasEl.style.cssText = lowerCanvasEl.style.cssText;
this._applyCanvasStyle(upperCanvasEl);
upperCanvasEl.setAttribute('draggable', 'true');
this.contextTop = upperCanvasEl.getContext(
'2d'
) as CanvasRenderingContext2D;
this.contextTop = upperCanvasEl.getContext('2d')!;
}

/**
* @private
*/
_createCacheCanvas() {
protected _createCacheCanvas() {
this.cacheCanvasEl = this._createCanvasElement();
this.cacheCanvasEl.setAttribute('width', `${this.width}`);
this.cacheCanvasEl.setAttribute('height', `${this.height}`);
Comment on lines -1262 to -1263
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size will be set by setDimensions on init

this.contextCache = this.cacheCanvasEl.getContext(
'2d'
) as CanvasRenderingContext2D;
this.contextCache = this.cacheCanvasEl.getContext('2d')!;
}

/**
* @private
*/
_initWrapperElement() {
if (this.wrapperEl) {
return;
}
protected _initWrapperElement() {
const container = getEnv().document.createElement('div');
container.classList.add(this.containerClass);
this.wrapperEl = wrapElement(this.lowerCanvasEl, container);
Expand All @@ -1292,7 +1259,7 @@ export class SelectableCanvas<
* @private
* @param {HTMLCanvasElement} element canvas element to apply styles on
*/
_applyCanvasStyle(element: HTMLCanvasElement) {
protected _applyCanvasStyle(element: HTMLCanvasElement) {
const width = this.width || element.width,
height = this.height || element.height;

Expand Down
91 changes: 27 additions & 64 deletions src/canvas/canvas_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,6 @@ export class Canvas extends SelectableCanvas {
*/
enablePointerEvents: boolean;

/**
* an internal flag that is used to remember if we already bound the events
* @type Boolean
* @private
*/
private eventsBound: boolean;

/**
* Holds a reference to a setTimeout timer for event synchronization
* @type number
Expand Down Expand Up @@ -123,17 +116,33 @@ export class Canvas extends SelectableCanvas {
*/
_previousPointer: Point;

/**
* Adds mouse listeners to canvas
* @private
*/
private _initEventListeners() {
// in case we initialized the class twice. This should not happen normally
// but in some kind of applications where the canvas element may be changed
// this is a workaround to having double listeners.
this.removeListeners();
this._bindEvents();
// @ts-ginore
constructor(el: string | HTMLCanvasElement, options = {}) {
super(el, options);
// bind event handlers
this._onMouseDown = this._onMouseDown.bind(this);
this._onTouchStart = this._onTouchStart.bind(this);
this._onMouseMove = this._onMouseMove.bind(this);
this._onMouseUp = this._onMouseUp.bind(this);
this._onTouchEnd = this._onTouchEnd.bind(this);
this._onResize = this._onResize.bind(this);
// this._onGesture=this._onGesture.bind(this)
// this._onDrag=this._onDrag.bind(this)
// this._onShake=this._onShake.bind(this)
// this._onLongPress=this._onLongPress.bind(this)
// this._onOrientationChange=this._onOrientationChange.bind(this)
this._onMouseWheel = this._onMouseWheel.bind(this);
this._onMouseOut = this._onMouseOut.bind(this);
this._onMouseEnter = this._onMouseEnter.bind(this);
this._onContextMenu = this._onContextMenu.bind(this);
this._onDoubleClick = this._onDoubleClick.bind(this);
this._onDragStart = this._onDragStart.bind(this);
this._onDragEnd = this._onDragEnd.bind(this);
this._onDragProgress = this._onDragProgress.bind(this);
this._onDragOver = this._onDragOver.bind(this);
this._onDragEnter = this._onDragEnter.bind(this);
this._onDragLeave = this._onDragLeave.bind(this);
this._onDrop = this._onDrop.bind(this);
// register event handlers
this.addOrRemove(addListener, 'add');
}

Expand Down Expand Up @@ -215,47 +224,6 @@ export class Canvas extends SelectableCanvas {
);
}

/**
* @private
*/
private _bindEvents() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to have this now that it is impossible to init canvas twice

if (this.eventsBound) {
// for any reason we pass here twice we do not want to bind events twice.
return;
}
(
[
'_onMouseDown',
'_onTouchStart',
'_onMouseMove',
'_onMouseUp',
'_onTouchEnd',
'_onResize',
// '_onGesture',
// '_onDrag',
// '_onShake',
// '_onLongPress',
// '_onOrientationChange',
'_onMouseWheel',
'_onMouseOut',
'_onMouseEnter',
'_onContextMenu',
'_onDoubleClick',
'_onDragStart',
'_onDragEnd',
'_onDragProgress',
'_onDragOver',
'_onDragEnter',
'_onDragLeave',
'_onDrop',
] as const
).forEach((eventHandler) => {
// @ts-expect-error dumb TS
this[eventHandler] = this[eventHandler].bind(this);
});
this.eventsBound = true;
}

/**
* @private
* @param {Event} [e] Event object fired on wheel event
Expand Down Expand Up @@ -1655,8 +1623,3 @@ export class Canvas extends SelectableCanvas {
return new Canvas(el);
}
}

// there is an order execution bug if i put this as public property.
Object.assign(Canvas.prototype, {
eventsBound: false,
});
Loading