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

CanvasView: Make the canvas the main element #111

Merged
merged 3 commits into from
Jul 25, 2020
Merged
Changes from 1 commit
Commits
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
48 changes: 26 additions & 22 deletions src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,28 +331,25 @@ class CanvasModel extends DOMWidgetModel {
export
class CanvasView extends DOMWidgetView {
render() {
this.canvas = document.createElement('canvas');

this.el.appendChild(this.canvas);
this.ctx = getContext(this.canvas);
this.ctx = getContext(this.el);

this.resizeCanvas();
this.model.on_some_change(['width', 'height'], this.resizeCanvas, this);

this.canvas.addEventListener('mousemove', { handleEvent: this.onMouseMove.bind(this) });
this.canvas.addEventListener('mousedown', { handleEvent: this.onMouseDown.bind(this) });
this.canvas.addEventListener('mouseup', { handleEvent: this.onMouseUp.bind(this) });
this.canvas.addEventListener('mouseout', { handleEvent: this.onMouseOut.bind(this) });
this.canvas.addEventListener('touchstart', { handleEvent: this.onTouchStart.bind(this) });
this.canvas.addEventListener('touchend', { handleEvent: this.onTouchEnd.bind(this) });
this.canvas.addEventListener('touchmove', { handleEvent: this.onTouchMove.bind(this) });
this.canvas.addEventListener('touchcancel', { handleEvent: this.onTouchCancel.bind(this) });
this.el.addEventListener('mousemove', { handleEvent: this.onMouseMove.bind(this) });
this.el.addEventListener('mousedown', { handleEvent: this.onMouseDown.bind(this) });
this.el.addEventListener('mouseup', { handleEvent: this.onMouseUp.bind(this) });
this.el.addEventListener('mouseout', { handleEvent: this.onMouseOut.bind(this) });
this.el.addEventListener('touchstart', { handleEvent: this.onTouchStart.bind(this) });
this.el.addEventListener('touchend', { handleEvent: this.onTouchEnd.bind(this) });
this.el.addEventListener('touchmove', { handleEvent: this.onTouchMove.bind(this) });
this.el.addEventListener('touchcancel', { handleEvent: this.onTouchCancel.bind(this) });

this.updateCanvas();
}

clear() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.ctx.clearRect(0, 0, this.el.width, this.el.height);
}

updateCanvas() {
Expand All @@ -361,8 +358,8 @@ class CanvasView extends DOMWidgetView {
}

protected resizeCanvas() {
this.canvas.setAttribute('width', this.model.get('width'));
this.canvas.setAttribute('height', this.model.get('height'));
this.el.setAttribute('width', this.model.get('width'));
this.el.setAttribute('height', this.model.get('height'));
}

private onMouseMove(event: MouseEvent) {
Expand Down Expand Up @@ -402,14 +399,18 @@ class CanvasView extends DOMWidgetView {
}

protected getCoordinates(event: MouseEvent | Touch) {
const rect = this.canvas.getBoundingClientRect();
const rect = this.el.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
martinRenou marked this conversation as resolved.
Show resolved Hide resolved

return { x, y };
}

canvas: HTMLCanvasElement;
get tagName(): string {
return 'canvas';
}

el: HTMLCanvasElement;
ctx: CanvasRenderingContext2D;

model: CanvasModel;
Expand Down Expand Up @@ -513,17 +514,20 @@ class MultiCanvasView extends DOMWidgetView {
// The following ts-ignore is needed due to ipywidgets's implementation
// @ts-ignore
return this.create_child_view(canvasModel).then((canvasView: CanvasView) => {
canvasView.el.style.zIndex = index;
const canvasContainer = document.createElement('div');

canvasContainer.style.zIndex = index.toString();

if (index == 0) {
// This will enforce the container to respect the children size.
canvasView.el.style.position = 'relative';
canvasView.el.style.float = 'left';
canvasContainer.style.position = 'relative';
canvasContainer.style.float = 'left';
} else {
canvasView.el.style.position = 'absolute';
canvasContainer.style.position = 'absolute';
}

this.container.appendChild(canvasView.el);
canvasContainer.appendChild(canvasView.el);
this.container.appendChild(canvasContainer);

return canvasView;
});
Expand Down