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