Verified Commit 45bebda6 authored by Thibaut Broggi's avatar Thibaut Broggi
Browse files

Cache images in javascript

      close #3
parent 5c55ce0b
let cachedImages = [];
let canvas = document.getElementById('output');
let ctx = canvas.getContext('2d');
......@@ -15,6 +17,17 @@ let get = function(url, cb) {
xmlHttp.send(null);
}
let getImage = function(id, cb) {
if (cachedImages[id])
cb(cachedImages[id]);
else {
get('./imgs/' + id + '.svg', function(res) {
cachedImages[id] = res;
cb(res);
});
}
}
let draw = function() {
let bgColor = document.querySelector('.colorpicker input[type="radio"][name="bg-color"]:checked').value;
let fgColor = document.querySelector('.colorpicker input[type="radio"][name="fg-color"]:checked').value;
......@@ -67,7 +80,7 @@ let draw = function() {
ctx.fillText('La Quadrature du Net !', offset, height * 0.9);
//Draw picto
get('./imgs/' + icon + '.svg', function(res) {
getImage(icon, function(res) {
let img = new Image();
let imgXml = res.replace(/#FFD201/g, fgColor);
img.src = 'data:image/svg+xml;base64,' + btoa(imgXml);
......
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