X-Git-Url: https://rosenzweig.io/git/?p=Dynartic.git;a=blobdiff_plain;f=DynImage.js;h=bb3605ea03ffd7d4c36ca723126febe389810ec1;hp=03ba4c9f60191679a0cb40dd87eddd984f88ad65;hb=932f922cb4f58a2a3c96f071681987e615007250;hpb=c5f2cee1c6dd704f333bf97a9128fb3b3c28212f diff --git a/DynImage.js b/DynImage.js index 03ba4c9..bb3605e 100644 --- a/DynImage.js +++ b/DynImage.js @@ -91,7 +91,7 @@ DynImage.prototype.fillColor = function(r, g, b, a) { // starting with a point, that point is raised a certain, specified amount (this is of course an analogy) // this causes the alpha value at that point to be raised to this value, // and ripples the effect the nearby pixels as well -DynImage.prototype.pullPoint = function(sx, sy, height, layerIntensity) { +DynImage.prototype.pointRing = function(sx, sy, height, layerIntensity, intensityMultiplier) { var rippleAmount = (255 / this.lesserDimension) * height; var usedPoints = {}; @@ -108,7 +108,7 @@ DynImage.prototype.pullPoint = function(sx, sy, height, layerIntensity) { usedPoints[x+";"+y] = 1; var distance = Math.sqrt( ( (sx - x) * (sx - x) ) + ( (sy - y) * (sy - y) ) ); - var intensity = (height - distance) / height; // linear function. + var intensity = Math.log(height - distance) * intensityMultiplier; // linear function. // this outputs a value 0-1, where 1 is the full effect of pulling and 0 is no effect // in the future, we may want a smoother curve // TODO: find alternative that works better @@ -125,4 +125,111 @@ DynImage.prototype.pullPoint = function(sx, sy, height, layerIntensity) { } } +DynImage.prototype.character2 = function(width, height) { + for(var x = 0; x < width; ++x) { + for(var y = 0; y < height; ++y) { + var color = this.getColor(x, y); + var mod = 110 + Math.floor(Math.random() * 40); + var mult = Math.ceil(Math.random() * 120); + + this.setColor(x, y, + Math.floor((color[0] * 0.5) + 128*Math.log(((x / width) * mod) + ((y / height) * mod))) & 255, + Math.floor((color[1] * 0.5) + 128*Math.log(((x / width) * mod) + ((y / height) * mod))) & 255, + Math.floor((color[2] * 0.5) + 128*Math.log(((x / width) * mod) + ((y / height) * mod))) & 255 + ); + } + } +} + +DynImage.prototype.punchImage = function(sx, sy, sw, sh, force, spread) { + for(var i = -(sw / 2); i < (sw/2); ++i) { + for(var j = (-(sh/2)); j < (sh/2); ++j) { + var x = sx + i, + y = sy + j; + + var intensity = Math.sqrt( (i*i) + (j*j) ); + intensity = Math.pow(2, -intensity / spread); + if( Math.sqrt( ((sx - x) * (sx - x)) + ((sy - y) * (sy - y))) < (sw/2)) + this.punchPoint(x, y, intensity * force); + } + } +} + +DynImage.prototype.punchWave = function(sx, sy, sw, sh, force, spread, period) { + for(var i = -(sw / 2); i < (sw/2); ++i) { + for(var j = (-(sh/2)); j < (sh/2); ++j) { + var x = sx + i, + y = sy + j; + + var intensity = Math.sqrt( (i*i) + (j*j) ); + intensity = Math.pow(2, -intensity / spread); + + this.punchPoint(x, y, intensity * force * Math.sin(intensity / period)); + } + } +} + +// opposite of point pulling; works on a single point only + +DynImage.prototype.punchPoint = function(x, y, force) { + var color = this.getColor(x, y); + color[0] -= force; + color[1] -= force; + color[2] -= force; + + color[0] = color[0] > 0 ? color[0] < 256 ? color[0] : 255 : 0; + color[1] = color[1] > 0 ? color[0] < 256 ? color[1] : 255 : 0; + color[2] = color[2] > 0 ? color[0] < 256 ? color[2] : 255 : 0; + + this.setColor(x, y, color[0], color[1], color[2], color[3]); +} + +// contrast changer +// any pixel below a threshold becomes darker, +// and any pixel above the threshold becomes lighter + +DynImage.prototype.recontrast = function(threshold, amount) { + for(var i = 0; i < this.width; ++i) { + for(var j = 0; j < this.height; ++j) { + var pix = this.getColor(i, j); + + // TODO: use HSL for proper value testing + + var avg = (pix[0] + pix[1] + pix[2]) / 3; + if(avg > threshold) { + this.setColor(i, j, pix[0] + amount, pix[1] + amount, pix[2] + amount); + } else { + this.setColor(i, j, pix[0] - amount, pix[1] - amount, pix[2] - amount); + } + } + } +} + +DynImage.prototype.borderPoint = function(x, y, bred, bgreen, bblue, percent) { + var color = this.getColor(x, y); + + // weighted average + + this.setColor( ((bred * percent) + (color[0] * (100 - percent))) / 200, + ((bgreen * percent) + (color[1] * (100 - percent))) / 200, + ((bblue * percent) + (color[2] * (100 - percent))) / 200); +} + +DynImage.prototype.border = function(width, height, bred, bgreen, bblue, mul) { + var borderIterations = mul; + + for(var i = 0; i < borderIterations; ++i) { + for(var x = i; x < (width - i); ++x) { + // top + + var y = i; + this.borderPoint(x, y, bred, bgreen, bblue, 100 - (i * mul)); + + y = height - i - 1; + this.borderPoint(x, y, bred, bgreen, bblue, 100 - (i * mul)); + } + } +} + + module.exports = DynImage;