@@ -331,28 +331,25 @@ class CanvasModel extends DOMWidgetModel {
331331export
332332class CanvasView extends DOMWidgetView {
333333 render ( ) {
334- this . canvas = document . createElement ( 'canvas' ) ;
335-
336- this . el . appendChild ( this . canvas ) ;
337- this . ctx = getContext ( this . canvas ) ;
334+ this . ctx = getContext ( this . el ) ;
338335
339336 this . resizeCanvas ( ) ;
340337 this . model . on_some_change ( [ 'width' , 'height' ] , this . resizeCanvas , this ) ;
341338
342- this . canvas . addEventListener ( 'mousemove' , { handleEvent : this . onMouseMove . bind ( this ) } ) ;
343- this . canvas . addEventListener ( 'mousedown' , { handleEvent : this . onMouseDown . bind ( this ) } ) ;
344- this . canvas . addEventListener ( 'mouseup' , { handleEvent : this . onMouseUp . bind ( this ) } ) ;
345- this . canvas . addEventListener ( 'mouseout' , { handleEvent : this . onMouseOut . bind ( this ) } ) ;
346- this . canvas . addEventListener ( 'touchstart' , { handleEvent : this . onTouchStart . bind ( this ) } ) ;
347- this . canvas . addEventListener ( 'touchend' , { handleEvent : this . onTouchEnd . bind ( this ) } ) ;
348- this . canvas . addEventListener ( 'touchmove' , { handleEvent : this . onTouchMove . bind ( this ) } ) ;
349- this . canvas . addEventListener ( 'touchcancel' , { handleEvent : this . onTouchCancel . bind ( this ) } ) ;
339+ this . el . addEventListener ( 'mousemove' , { handleEvent : this . onMouseMove . bind ( this ) } ) ;
340+ this . el . addEventListener ( 'mousedown' , { handleEvent : this . onMouseDown . bind ( this ) } ) ;
341+ this . el . addEventListener ( 'mouseup' , { handleEvent : this . onMouseUp . bind ( this ) } ) ;
342+ this . el . addEventListener ( 'mouseout' , { handleEvent : this . onMouseOut . bind ( this ) } ) ;
343+ this . el . addEventListener ( 'touchstart' , { handleEvent : this . onTouchStart . bind ( this ) } ) ;
344+ this . el . addEventListener ( 'touchend' , { handleEvent : this . onTouchEnd . bind ( this ) } ) ;
345+ this . el . addEventListener ( 'touchmove' , { handleEvent : this . onTouchMove . bind ( this ) } ) ;
346+ this . el . addEventListener ( 'touchcancel' , { handleEvent : this . onTouchCancel . bind ( this ) } ) ;
350347
351348 this . updateCanvas ( ) ;
352349 }
353350
354351 clear ( ) {
355- this . ctx . clearRect ( 0 , 0 , this . canvas . width , this . canvas . height ) ;
352+ this . ctx . clearRect ( 0 , 0 , this . el . width , this . el . height ) ;
356353 }
357354
358355 updateCanvas ( ) {
@@ -361,8 +358,8 @@ class CanvasView extends DOMWidgetView {
361358 }
362359
363360 protected resizeCanvas ( ) {
364- this . canvas . setAttribute ( 'width' , this . model . get ( 'width' ) ) ;
365- this . canvas . setAttribute ( 'height' , this . model . get ( 'height' ) ) ;
361+ this . el . setAttribute ( 'width' , this . model . get ( 'width' ) ) ;
362+ this . el . setAttribute ( 'height' , this . model . get ( 'height' ) ) ;
366363 }
367364
368365 private onMouseMove ( event : MouseEvent ) {
@@ -402,14 +399,19 @@ class CanvasView extends DOMWidgetView {
402399 }
403400
404401 protected getCoordinates ( event : MouseEvent | Touch ) {
405- const rect = this . canvas . getBoundingClientRect ( ) ;
406- const x = event . clientX - rect . left ;
407- const y = event . clientY - rect . top ;
402+ const rect = this . el . getBoundingClientRect ( ) ;
403+
404+ const x = this . el . width * ( event . clientX - rect . left ) / rect . width ;
405+ const y = this . el . height * ( event . clientY - rect . top ) / rect . height ;
408406
409407 return { x, y } ;
410408 }
411409
412- canvas : HTMLCanvasElement ;
410+ get tagName ( ) : string {
411+ return 'canvas' ;
412+ }
413+
414+ el : HTMLCanvasElement ;
413415 ctx : CanvasRenderingContext2D ;
414416
415417 model : CanvasModel ;
@@ -513,17 +515,20 @@ class MultiCanvasView extends DOMWidgetView {
513515 // The following ts-ignore is needed due to ipywidgets's implementation
514516 // @ts -ignore
515517 return this . create_child_view ( canvasModel ) . then ( ( canvasView : CanvasView ) => {
516- canvasView . el . style . zIndex = index ;
518+ const canvasContainer = document . createElement ( 'div' ) ;
519+
520+ canvasContainer . style . zIndex = index . toString ( ) ;
517521
518522 if ( index == 0 ) {
519523 // This will enforce the container to respect the children size.
520- canvasView . el . style . position = 'relative' ;
521- canvasView . el . style . float = 'left' ;
524+ canvasContainer . style . position = 'relative' ;
525+ canvasContainer . style . float = 'left' ;
522526 } else {
523- canvasView . el . style . position = 'absolute' ;
527+ canvasContainer . style . position = 'absolute' ;
524528 }
525529
526- this . container . appendChild ( canvasView . el ) ;
530+ canvasContainer . appendChild ( canvasView . el ) ;
531+ this . container . appendChild ( canvasContainer ) ;
527532
528533 return canvasView ;
529534 } ) ;
0 commit comments