-
Notifications
You must be signed in to change notification settings - Fork 81
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
Add set canvas element #534
Changes from 3 commits
e0a4331
c8c3a8d
c3cccbf
40be774
bf86afe
b48bea3
dba3429
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 @@ export interface HtmlCanvasApi extends PluginApi { | |
get element(): HTMLCanvasElement | ||
/** Returns the actual canvas context */ | ||
get context(): CanvasRenderingContext2D | ||
set element(element: HTMLCanvasElement) | ||
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. Please add comment (needed for generated reference documentation) |
||
clientToCanvas(clientPos: Geom.Point): Geom.Point | ||
canvasToClient(renderPos: Geom.Point): Geom.Point | ||
} | ||
|
@@ -29,9 +30,9 @@ export class HtmlCanvas implements Plugin, HtmlCanvasAlternative { | |
private _container?: HTMLElement | ||
private _offscreenCanvas: HTMLCanvasElement | ||
private _offscreenContext: CanvasRenderingContext2D | ||
private _mainCanvas: HTMLCanvasElement | ||
private _context: CanvasRenderingContext2D | ||
private _resizeObserver: ResizeObserver | ||
private _mainCanvas!: HTMLCanvasElement | ||
simzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private _context!: CanvasRenderingContext2D | ||
private _resizeObserver!: ResizeObserver | ||
private _resizeHandler: () => void | ||
private _prevUpdateHash: string = '' | ||
private _scaleFactor: number = 1 | ||
|
@@ -44,6 +45,7 @@ export class HtmlCanvas implements Plugin, HtmlCanvasAlternative { | |
return { | ||
element: this.element, | ||
context: this.context, | ||
setElement: this._setElement.bind(this), | ||
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. It would be better to have getter setter for element here:
Also calling _setElement() here is faulty, since it does not register the handlers to the canvas, just stores the element to the class. |
||
clientToCanvas: this._clientToCanvas.bind(this), | ||
canvasToClient: this._canvasToClient.bind(this) | ||
} | ||
|
@@ -102,20 +104,30 @@ export class HtmlCanvas implements Plugin, HtmlCanvasAlternative { | |
} | ||
|
||
constructor(options: CanvasOptions) { | ||
this._mainCanvas = this._toCanvasElement(options.element) | ||
this._offscreenCanvas = document.createElement<'canvas'>('canvas') | ||
const offCtx = this._offscreenCanvas.getContext('2d') | ||
if (!offCtx) throw Error('Cannot get rendering context of internal canvas') | ||
this._offscreenContext = offCtx | ||
this._resizeHandler = (): void => { | ||
this._update(true) | ||
} | ||
this._setCanvas(options.element) | ||
window.addEventListener('resize', this._resizeHandler) | ||
} | ||
|
||
private _setCanvas(element: HTMLElement): void { | ||
this._mainCanvas = this._toCanvasElement(element) | ||
this._prevUpdateHash = '' | ||
const ctx = this._mainCanvas.getContext('2d') | ||
if (!ctx) throw Error('Cannot get rendering context of canvas') | ||
this._context = ctx | ||
this._calcSize() | ||
this._resizeObserver = this._createResizeObserverFor(this._mainCanvas) | ||
this._resizeHandler = (): void => { | ||
this._update(true) | ||
} | ||
window.addEventListener('resize', this._resizeHandler) | ||
} | ||
|
||
private _setElement(element: HTMLCanvasElement): void { | ||
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. This method is not necessary, we should cann _setCanvas() for the API object. |
||
this.element = element | ||
|
||
} | ||
|
||
unregister(): void { | ||
|
@@ -128,6 +140,11 @@ export class HtmlCanvas implements Plugin, HtmlCanvasAlternative { | |
return this._mainCanvas | ||
} | ||
|
||
set element(element: HTMLCanvasElement) { | ||
this.unregister() | ||
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 shouldn't call unregister here, since it also unregisters the window resize event listener, which we need further on. |
||
this._setCanvas(element) | ||
} | ||
|
||
get context(): CanvasRenderingContext2D { | ||
return this._offscreenContext | ||
} | ||
|
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.
typo: cahange -> change