Created
January 16, 2011 05:31
-
-
Save ideamonk/781591 to your computer and use it in GitHub Desktop.
A bit imperfect stickiness implementation resulted from aims of reproducing intelligent scissors, but making distance weights dynamic might help emulate what Mortensen aims at. Edge finding hardly helps though.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Author: ideamonk | |
*/ | |
window.scissors = { | |
MAX_GRADIENT:179.606, | |
GRADIENT_SEARCH:32, | |
radius:16, | |
MIN_GRADIENT:63, | |
COST_WIDTH:2, | |
distance_weights:[], | |
TOLERANCE:3, | |
}; | |
function clamp(x,l,u){ | |
return (x<l) ? l:( (x>u) ? u:x ); | |
} | |
scissors.build_weights = function(){ | |
for (i=0; i<scissors.GRADIENT_SEARCH; i++){ | |
for (j=0; j<scissors.GRADIENT_SEARCH; j++){ | |
scissors.distance_weights.push( | |
1.0 / (1 + Math.sqrt((i-scissors.radius)*(i-scissors.radius) + (j-scissors.radius)*(j-scissors.radius))) | |
); | |
} | |
} | |
} | |
scissors.find_max_gradient = function(x,y){ | |
var cx = clamp(x,0,scissors.canvas2.width); | |
var cy = clamp(y,0,scissors.canvas2.height); | |
var sx = cx - scissors.radius; | |
var sy = cy - scissors.radius; | |
var x1 = clamp( cx-scissors.radius, 0, scissors.canvas2.width); | |
var y1 = clamp( cy-scissors.radius, 0, scissors.canvas2.height); | |
var x2 = clamp( cx+scissors.radius, 0, scissors.canvas2.width); | |
var y2 = clamp( cy+scissors.radius, 0, scissors.canvas2.height); | |
var max_gradient = 0, gradient=0, g=0; | |
var rx = cx, ry=cy; | |
//scissors.context.fillStyle="rgba(255,255,0,0.1)"; | |
//scissors.context.fillRect(x1,y1, x2-x1, y2-y1); | |
for (var i = y1; i<y2; i++){ | |
for (var j = x1; j<x2; j++){ | |
gradient = scissors.distance_weights[(i-y1) * scissors.GRADIENT_SEARCH + (j-x1)] * scissors.context2.getImageData(j, i, 1, 1).data[1]; | |
if (gradient>max_gradient && gradient>scissors.TOLERANCE){ | |
max_gradient=gradient; | |
rx=j; | |
ry=i; | |
console.log(gradient); | |
//scissors.context.fillStyle="#00f"; | |
//scissors.context.fillRect(rx, ry, 1, 1); | |
} | |
} | |
} | |
//scissors.context.fillStyle="#f00"; | |
//scissors.context.fillRect(rx, ry, 3, 3); | |
//scissors.context.stroke(); | |
return [rx,ry]; | |
} | |
$(document).ready( function(){ | |
var img = document.getElementById("myMap"); | |
scissors.canvas = document.getElementById("myCanvas"); | |
scissors.canvas2 = document.createElement("canvas"); | |
document.body.appendChild(scissors.canvas2); | |
scissors.canvas2.width = scissors.canvas.width; | |
scissors.canvas2.height = scissors.canvas.height; | |
scissors.context2 = scissors.canvas2.getContext("2d"); | |
scissors.context = scissors.canvas.getContext("2d"); | |
img.onload = function(){ | |
scissors.context.drawImage(this,0,0); | |
} | |
Pixastic.process(img, "edges", {mono:true},function(){ | |
scissors.context2.drawImage(document.getElementById("myMap"),0,0); | |
}); | |
//Pixastic.process(img, "blurfast", {amount:0.3}); | |
scissors.down = false; | |
scissors.build_weights(); | |
// MOUSEDONE | |
function canvas_mouseDown(e){ | |
scissors.down = true; | |
var x = e.pageX - document.getElementById("myMap").offsetLeft; | |
var y = e.pageY; | |
var result = scissors.find_max_gradient(x,y); | |
scissors._x = result[0]; | |
scissors._y = result[1]; | |
scissors.context.moveTo(scissors._x, scissors._y); | |
} | |
// MOUSEMOVE | |
function canvas_mouseMove(e){ | |
if (scissors.down){ | |
var x = e.pageX - document.getElementById("myMap").offsetLeft; | |
var y = e.pageY; | |
var result = scissors.find_max_gradient(x,y); | |
//scissors.context2.line(scissors._x, scissors._y, result[0], result[1]); | |
scissors._x = result[0]; | |
scissors._y = result[1]; | |
scissors.context.lineTo(scissors._x, scissors._y); | |
scissors.context.stroke(); | |
} | |
} | |
// MOUSEUP | |
function canvas_mouseUp(e){ | |
scissors.down = false; | |
} | |
// setup events | |
scissors.canvas.addEventListener("mousedown", canvas_mouseDown, false ); | |
scissors.canvas.addEventListener("mousemove", canvas_mouseMove, false ); | |
scissors.canvas.addEventListener("mouseup", canvas_mouseUp, false ); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
:D