Commit a759bcd0 authored by Daniel Hosseinian's avatar Daniel Hosseinian Committed by Commit Bot

Dynamically create/remove canvas elements for PDF thumbnails

Instead of just clearing the canvas when going out of view, remove it.
Recreate the canvas once needed again.

Bug: 1134057
Change-Id: I11131d7bf584eb51c94f4fd24484ba9f6675e75e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2451387Reviewed-by: default avatardpapad <dpapad@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Cr-Commit-Position: refs/heads/master@{#814803}
parent 73f349b7
...@@ -11,10 +11,12 @@ ...@@ -11,10 +11,12 @@
#thumbnail { #thumbnail {
align-items: center; align-items: center;
display: inline-flex; display: inline-flex;
height: 140px;
justify-content: center; justify-content: center;
margin-bottom: 12px; margin-bottom: 12px;
margin-inline-end: auto; margin-inline-end: auto;
margin-inline-start: auto; margin-inline-start: auto;
width: 108px;
} }
:host([is-active]) #thumbnail { :host([is-active]) #thumbnail {
...@@ -49,7 +51,5 @@ ...@@ -49,7 +51,5 @@
line-height: 1; line-height: 1;
} }
</style> </style>
<div id="thumbnail" on-click="onClick_"> <div id="thumbnail" on-click="onClick_"></div>
<canvas width="108" height="140"></canvas>
</div>
<div id="pageNumber">[[pageNumber]]</div> <div id="pageNumber">[[pageNumber]]</div>
...@@ -33,6 +33,7 @@ export class ViewerThumbnailElement extends PolymerElement { ...@@ -33,6 +33,7 @@ export class ViewerThumbnailElement extends PolymerElement {
clockwiseRotations: { clockwiseRotations: {
type: Number, type: Number,
value: 0, value: 0,
observer: 'clockwiseRotationsChanged_',
}, },
isActive: { isActive: {
...@@ -45,12 +46,6 @@ export class ViewerThumbnailElement extends PolymerElement { ...@@ -45,12 +46,6 @@ export class ViewerThumbnailElement extends PolymerElement {
}; };
} }
static get observers() {
return [
'styleCanvas_(clockwiseRotations)',
];
}
constructor() { constructor() {
super(); super();
...@@ -61,7 +56,12 @@ export class ViewerThumbnailElement extends PolymerElement { ...@@ -61,7 +56,12 @@ export class ViewerThumbnailElement extends PolymerElement {
/** @param {!ImageData} imageData */ /** @param {!ImageData} imageData */
set image(imageData) { set image(imageData) {
const canvas = this.getCanvas_(); let canvas = this.getCanvas_();
if (!canvas) {
canvas = document.createElement('canvas');
this.shadowRoot.querySelector('#thumbnail').appendChild(canvas);
}
canvas.width = imageData.width; canvas.width = imageData.width;
canvas.height = imageData.height; canvas.height = imageData.height;
...@@ -76,9 +76,12 @@ export class ViewerThumbnailElement extends PolymerElement { ...@@ -76,9 +76,12 @@ export class ViewerThumbnailElement extends PolymerElement {
return; return;
} }
// `canvas` can be `null` in tests because `image` is set only in response
// to the plugin.
const canvas = this.getCanvas_(); const canvas = this.getCanvas_();
const ctx = canvas.getContext('2d'); if (canvas) {
ctx.clearRect(0, 0, canvas.width, canvas.height); canvas.remove();
}
this.removeAttribute(PAINTED_ATTRIBUTE); this.removeAttribute(PAINTED_ATTRIBUTE);
// For tests // For tests
...@@ -91,12 +94,19 @@ export class ViewerThumbnailElement extends PolymerElement { ...@@ -91,12 +94,19 @@ export class ViewerThumbnailElement extends PolymerElement {
this.shadowRoot.querySelector('#thumbnail')); this.shadowRoot.querySelector('#thumbnail'));
} }
/** @private */
clockwiseRotationsChanged_() {
if (this.getCanvas_()) {
this.styleCanvas_();
}
}
/** /**
* @return {!HTMLCanvasElement} * @return {?HTMLCanvasElement}
* @private * @private
*/ */
getCanvas_() { getCanvas_() {
return /** @type {!HTMLCanvasElement} */ ( return /** @type {?HTMLCanvasElement} */ (
this.shadowRoot.querySelector('canvas')); this.shadowRoot.querySelector('canvas'));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment