Skip to content
Snippets Groups Projects
Commit 19f52a09 authored by Tomás Vega's avatar Tomás Vega
Browse files

horizontal flip for double sided boards

parent 6e07f046
Branches
No related tags found
No related merge requests found
......@@ -5,7 +5,7 @@
// (c) Massachusetts Institute of Technology 2015,6
//
// This work may be reproduced, modified, distributed, performed, and
// displayed for any purpose, but must acknowledge the mods
// displayed for any purpose, but must acknowledge the fab modules
// project. Copyright is retained and must be preserved. The work is
// provided as is; no warranty is provided, and users accept all
// liability.
......@@ -139,6 +139,18 @@ var interface = function(div){
div.appendChild(btn)
div.appendChild(document.createElement('br'))
//
// flip button
//
var flip_btn = document.createElement('button')
flip_btn.style.padding = mods.ui.padding
flip_btn.style.margin = 1
flip_btn.appendChild(document.createTextNode('flip'))
flip_btn.addEventListener('click',function(){
flip_image()
})
div.appendChild(flip_btn)
div.appendChild(document.createElement('br'))
//
// info div
//
var info = document.createElement('div')
......@@ -334,10 +346,83 @@ function worker() {
})
}
//
// flip image
//
function flip_image() {
var blob = new Blob(['('+flip_worker.toString()+'())'])
var url = window.URL.createObjectURL(blob)
var webworker = new Worker(url)
webworker.addEventListener('message',function(evt) {
window.URL.revokeObjectURL(url)
var h = mod.img.height
var w = mod.img.width
var buf = new Uint8ClampedArray(evt.data.buffer)
var imgdata = new ImageData(buf,w,h)
var ctx = mod.img.getContext("2d")
ctx.putImageData(imgdata,0,0)
if (w > h) {
var x0 = 0
var y0 = mod.canvas.height*.5*(1-h/w)
var wd = mod.canvas.width
var hd = mod.canvas.width*h/w
}
else {
var x0 = mod.canvas.width*.5*(1-w/h)
var y0 = 0
var wd = mod.canvas.height*w/h
var hd = mod.canvas.height
}
var ctx = mod.canvas.getContext("2d")
ctx.drawImage(mod.img,x0,y0,wd,hd)
webworker.terminate()
outputs.image.event()
})
var ctx = mod.canvas.getContext("2d")
ctx.clearRect(0,0,mod.canvas.width,mod.canvas.height)
var h = mod.img.height
var w = mod.img.width
var ctx = mod.img.getContext("2d")
var img = ctx.getImageData(0,0,w,h)
webworker.postMessage({
height:img.height,width:img.width,buffer:img.data.buffer},
[img.data.buffer])
}
function flip_worker() {
self.addEventListener('message',function(evt) {
var h = evt.data.height
var w = evt.data.width
var buf = new Uint8ClampedArray(evt.data.buffer)
var index = 0
for (var row = 0; row < h; ++row) {
for (var col = 0; col < w/2; ++col) {
index = (h-1-row)*w*4+col*4
r_index = (h-1-row)*w*4+(w-1-col)*4
temp_1 = buf[index+0]
temp_2 = buf[index+1]
temp_3 = buf[index+2]
buf[index+0]
= buf[r_index+0]
buf[index+1]
= buf[r_index+1]
buf[index+2]
= buf[r_index+2]
buf[r_index+0]
= temp_1
buf[r_index+1]
= temp_2
buf[r_index+2]
= temp_3
buf[index+3] = 255
buf[r_index+3] = 255
}
}
self.postMessage({buffer:buf.buffer},[buf.buffer])
})
}
//
// return values
//
return ({
mod:mod,
name:name,
init:init,
inputs:inputs,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment