comparison cpack/dml/web/js/smartimg.js @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:718306e29690
1 /* Part of DML (Digital Music Laboratory)
2 Copyright 2014-2015 Samer Abdallah, University College London
3 Distributed under GPL v3
4 */
5 function load_img(id,url)
6 {
7 var xhr = new XMLHttpRequest();
8 xhr.open('GET',url,true);
9 xhr.responseType = 'arraybuffer';
10 xhr.onload = function(e) {
11 var type = xhr.getResponseHeader('Content-Type')
12 console.log(type)
13
14 var arr = new Uint8Array(this.response);
15 var raw = '';
16 var i,j,subArray,chunk = 5000;
17 for (i=0,j=arr.length; i<j; i+=chunk) {
18 subArray = arr.subarray(i,i+chunk);
19 raw += String.fromCharCode.apply(null, subArray);
20 }
21 var dataURL="data:"+type+";base64,"+btoa(raw);
22 document.getElementById(id).src = dataURL;
23 };
24 xhr.send();
25 }
26
27 function load_img2(id,url)
28 {
29 var xhr = new XMLHttpRequest();
30 xhr.responseType = 'arraybuffer';
31 xhr.onload = function() {
32 var type = xhr.getResponseHeader('Content-Type')
33 var el = document.getElementById(id);
34 if (type.substring(0,6)=="image/") {
35 var blb = new Blob([xhr.response], {type: type});
36 var url = (window.URL || window.webkitURL).createObjectURL(blb);
37 el.src = url;
38 } else {
39 var styles = window.getComputedStyle(el);
40 var fr = document.createElement('iframe');
41 var st = fr.style;
42 st.width=styles.getPropertyValue('width');
43 st.height=styles.getPropertyValue('height');
44 st.display='inline';
45 st['vertical-align']='middle';
46 st['background-color']='#ffe0e0';
47
48 el.parentNode.replaceChild(fr, el);
49 var arr = new Uint8Array(this.response);
50 var raw = String.fromCharCode.apply(null, arr);
51 var doc = fr.contentWindow.document;
52 doc.open();
53 doc.write(raw);
54 doc.close();
55 doc.body.style['font-size']='80%'
56 }
57 }
58 xhr.open('GET',url);
59 xhr.send();
60 }
61