comparison client/src/components/gauge/Waterlevel.vue @ 3787:f86220aa8a72 yworks-svg2pdf

Waterlevel: prefetch images of template and convert to dataURI
author Thomas Junk <thomas.junk@intevation.de>
date Wed, 03 Jul 2019 16:42:38 +0200
parents d36ccff8de5f
children d428f283fe10
comparison
equal deleted inserted replaced
3786:a2da2328bb86 3787:f86220aa8a72
386 "X-Gemma-Auth": localStorage.getItem("token"), 386 "X-Gemma-Auth": localStorage.getItem("token"),
387 "Content-type": "text/xml; charset=UTF-8" 387 "Content-type": "text/xml; charset=UTF-8"
388 } 388 }
389 }) 389 })
390 .then(response => { 390 .then(response => {
391 this.templateData = response.data.template_data; 391 /**
392 * In order to render the images from the template, we need to convert
393 * each image to dataURIs. Since this happens asynchronous,
394 * we need to wrap each image into its own promise and only after all are
395 * finished, we continue with the flow.
396 */
397 const imageElementLoaders = response.data.template_data.elements.reduce(
398 (o, n, i) => {
399 if (n.type === "image") {
400 o.push(
401 new Promise(resolve => {
402 const image = new Image();
403 image.onload = function() {
404 var canvas = document.createElement("canvas");
405 canvas.width = this.naturalWidth; // or 'width' if you want a special/scaled size
406 canvas.height = this.naturalHeight; // or 'height' if you want a special/scaled size
407 canvas.getContext("2d").drawImage(this, 0, 0);
408 resolve({
409 index: i,
410 url: canvas.toDataURL("image/png")
411 });
412 };
413 image.src = n.url;
414 })
415 );
416 }
417 return o;
418 },
419 []
420 );
421 Promise.all(imageElementLoaders).then(values => {
422 values.forEach(v => {
423 response.data.template_data.elements[v.index].url = v.url;
424 });
425 this.templateData = response.data.template_data;
426 });
392 }) 427 })
393 .catch(e => { 428 .catch(e => {
394 const { status, data } = e.response; 429 const { status, data } = e.response;
395 displayError({ 430 displayError({
396 title: this.$gettext("Backend Error"), 431 title: this.$gettext("Backend Error"),