admin管理员组文章数量:1296893
I have drawing app something like fabric.js ( / )
I want to embed the blurring tool like Photoshop ( .php?id=1503&series=85&format=html )
This is my blurring function but it not working fine when i am try to change color it is going something wrong you can see screenshots below ...
function boxBlurCanvasRGBA( id, top_x, top_y, width, height, radius, iterations ){
if ( isNaN(radius) || radius < 1 ) return;
radius |= 0;
if ( isNaN(iterations) ) iterations = 1;
iterations |= 0;
if ( iterations > 3 ) iterations = 3;
if ( iterations < 1 ) iterations = 1;
var canvas = document.getElementById( 'paper' );
var context = canvas.getContext("2d");
var imageData;
try {
try {
imageData = context.getImageData( top_x, top_y, width, height );
} catch(e) {
// NOTE: this part is supposedly only needed if you want to work with local files
// so it might be okay to remove the whole try/catch block and just use
// imageData = context.getImageData( top_x, top_y, width, height );
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
imageData = context.getImageData( top_x, top_y, width, height );
} catch(e) {
alert("Cannot access local image");
throw new Error("unable to access local image data: " + e);
return;
}
}
} catch(e) {
alert("Cannot access image");
throw new Error("unable to access image data: " + e);
return;
}
var pixels = imageData.data;
var rsum,gsum,bsum,asum,x,y,i,p,p1,p2,yp,yi,yw,idx,pa;
var wm = width - 1;
var hm = height - 1;
var wh = width * height;
var rad1 = radius + 1;
var mul_sum = mul_table[radius];
var shg_sum = shg_table[radius];
var r = [];
var g = [];
var b = [];
var a = [];
var vmin = [];
var vmax = [];
while ( iterations-- > 0 ){
yw = yi = 0;
for ( y=0; y < height; y++ ){
rsum = pixels[yw] * rad1;
gsum = pixels[yw+1] * rad1;
bsum = pixels[yw+2] * rad1;
asum = pixels[yw+3] * rad1;
for( i = 1; i <= radius; i++ ){
p = yw + (((i > wm ? wm : i )) << 2 );
rsum += pixels[p++];
gsum += pixels[p++];
bsum += pixels[p++];
asum += pixels[p]
}
for ( x = 0; x < width; x++ ) {
r[yi] = rsum;
g[yi] = gsum;
b[yi] = bsum;
a[yi] = asum;
if( y==0) {
vmin[x] = ( ( p = x + rad1) < wm ? p : wm ) << 2;
vmax[x] = ( ( p = x - radius) > 0 ? p << 2 : 0 );
}
p1 = yw + vmin[x];
p2 = yw + vmax[x];
rsum += pixels[p1++] - pixels[p2++];
gsum += pixels[p1++] - pixels[p2++];
bsum += pixels[p1++] - pixels[p2++];
asum += pixels[p1] - pixels[p2];
yi++;
}
yw += ( width << 2 );
}
for ( x = 0; x < width; x++ ) {
yp = x;
rsum = r[yp] * rad1;
gsum = g[yp] * rad1;
bsum = b[yp] * rad1;
asum = a[yp] * rad1;
for( i = 1; i <= radius; i++ ) {
yp += ( i > hm ? 0 : width );
rsum += r[yp];
gsum += g[yp];
bsum += b[yp];
asum += a[yp];
}
yi = x << 2;
for ( y = 0; y < height; y++) {
pixels[yi+3] = pa = (asum * mul_sum) >>> shg_sum;
if ( pa > 0 )
{
pa = 255 / pa;
pixels[yi] = ((rsum * mul_sum) >>> shg_sum) * pa;
pixels[yi+1] = ((gsum * mul_sum) >>> shg_sum) * pa;
pixels[yi+2] = ((bsum * mul_sum) >>> shg_sum) * pa;
} else {
pixels[yi] = pixels[yi+1] = pixels[yi+2] = 0;
}
if( x == 0 ) {
vmin[y] = ( ( p = y + rad1) < hm ? p : hm ) * width;
vmax[y] = ( ( p = y - radius) > 0 ? p * width : 0 );
}
p1 = x + vmin[y];
p2 = x + vmax[y];
rsum += r[p1] - r[p2];
gsum += g[p1] - g[p2];
bsum += b[p1] - b[p2];
asum += a[p1] - a[p2];
yi += width << 2;
}
}
}
context.globalAlpha = 0.8;
context.putImageData( imageData, top_x, top_y );
}
can any one give me some code example ?
I have drawing app something like fabric.js ( http://fabricjs./freedrawing/ )
I want to embed the blurring tool like Photoshop ( http://www.demowolf./tutorials/demo.php?id=1503&series=85&format=html )
This is my blurring function but it not working fine when i am try to change color it is going something wrong you can see screenshots below ...
function boxBlurCanvasRGBA( id, top_x, top_y, width, height, radius, iterations ){
if ( isNaN(radius) || radius < 1 ) return;
radius |= 0;
if ( isNaN(iterations) ) iterations = 1;
iterations |= 0;
if ( iterations > 3 ) iterations = 3;
if ( iterations < 1 ) iterations = 1;
var canvas = document.getElementById( 'paper' );
var context = canvas.getContext("2d");
var imageData;
try {
try {
imageData = context.getImageData( top_x, top_y, width, height );
} catch(e) {
// NOTE: this part is supposedly only needed if you want to work with local files
// so it might be okay to remove the whole try/catch block and just use
// imageData = context.getImageData( top_x, top_y, width, height );
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
imageData = context.getImageData( top_x, top_y, width, height );
} catch(e) {
alert("Cannot access local image");
throw new Error("unable to access local image data: " + e);
return;
}
}
} catch(e) {
alert("Cannot access image");
throw new Error("unable to access image data: " + e);
return;
}
var pixels = imageData.data;
var rsum,gsum,bsum,asum,x,y,i,p,p1,p2,yp,yi,yw,idx,pa;
var wm = width - 1;
var hm = height - 1;
var wh = width * height;
var rad1 = radius + 1;
var mul_sum = mul_table[radius];
var shg_sum = shg_table[radius];
var r = [];
var g = [];
var b = [];
var a = [];
var vmin = [];
var vmax = [];
while ( iterations-- > 0 ){
yw = yi = 0;
for ( y=0; y < height; y++ ){
rsum = pixels[yw] * rad1;
gsum = pixels[yw+1] * rad1;
bsum = pixels[yw+2] * rad1;
asum = pixels[yw+3] * rad1;
for( i = 1; i <= radius; i++ ){
p = yw + (((i > wm ? wm : i )) << 2 );
rsum += pixels[p++];
gsum += pixels[p++];
bsum += pixels[p++];
asum += pixels[p]
}
for ( x = 0; x < width; x++ ) {
r[yi] = rsum;
g[yi] = gsum;
b[yi] = bsum;
a[yi] = asum;
if( y==0) {
vmin[x] = ( ( p = x + rad1) < wm ? p : wm ) << 2;
vmax[x] = ( ( p = x - radius) > 0 ? p << 2 : 0 );
}
p1 = yw + vmin[x];
p2 = yw + vmax[x];
rsum += pixels[p1++] - pixels[p2++];
gsum += pixels[p1++] - pixels[p2++];
bsum += pixels[p1++] - pixels[p2++];
asum += pixels[p1] - pixels[p2];
yi++;
}
yw += ( width << 2 );
}
for ( x = 0; x < width; x++ ) {
yp = x;
rsum = r[yp] * rad1;
gsum = g[yp] * rad1;
bsum = b[yp] * rad1;
asum = a[yp] * rad1;
for( i = 1; i <= radius; i++ ) {
yp += ( i > hm ? 0 : width );
rsum += r[yp];
gsum += g[yp];
bsum += b[yp];
asum += a[yp];
}
yi = x << 2;
for ( y = 0; y < height; y++) {
pixels[yi+3] = pa = (asum * mul_sum) >>> shg_sum;
if ( pa > 0 )
{
pa = 255 / pa;
pixels[yi] = ((rsum * mul_sum) >>> shg_sum) * pa;
pixels[yi+1] = ((gsum * mul_sum) >>> shg_sum) * pa;
pixels[yi+2] = ((bsum * mul_sum) >>> shg_sum) * pa;
} else {
pixels[yi] = pixels[yi+1] = pixels[yi+2] = 0;
}
if( x == 0 ) {
vmin[y] = ( ( p = y + rad1) < hm ? p : hm ) * width;
vmax[y] = ( ( p = y - radius) > 0 ? p * width : 0 );
}
p1 = x + vmin[y];
p2 = x + vmax[y];
rsum += r[p1] - r[p2];
gsum += g[p1] - g[p2];
bsum += b[p1] - b[p2];
asum += a[p1] - a[p2];
yi += width << 2;
}
}
}
context.globalAlpha = 0.8;
context.putImageData( imageData, top_x, top_y );
}
can any one give me some code example ?
Share edited Mar 24, 2014 at 9:26 VostanAzatyan asked Mar 24, 2014 at 9:02 VostanAzatyanVostanAzatyan 6472 gold badges9 silver badges23 bronze badges 3- Remending tools or giving you code is not what StackOverflow is for. Post up the code you've tried, and we'll assist you. – James Donnelly Commented Mar 24, 2014 at 9:17
- i have added the code you can see it – VostanAzatyan Commented Mar 24, 2014 at 9:26
- This article on image filters may help you in convolving images. html5rocks./en/tutorials/canvas/imagefilters – Tank Commented Mar 24, 2014 at 17:27
1 Answer
Reset to default 10Interesting question!
Here's how to implement a blur brush using:
- an offscreen canvas
- a blur-effect algorithm
- positing to "clip" an image to fit in the user's brush-strokes
- positing to "draw-behind" the clear image behind the blurred brushstroke image.
A Demo: http://jsfiddle/m1erickson/baDLp/
- create an offscreen canvas
- let the user draw on the onscreen canvas with a brush
- simultaneously draw the same brush-strokes on the offscreen canvas
- after the user is finished brushing...
- set the offscreen canvas positing to "source-in" (any new drawing is only on the existing brushstrokes.
- drawImage the image on the offscreen canvas (the image will be drawn only on the brushstrokes)
- use a blur algorithm to blur the offscreen canvas (at this point the offscreen canvas contains a blurred image only on the brushstrokes)
- clear the onscreen canvas
- use drawImage to copy the blurred brush-image from the offscreen canvas to the onscreen canvas.
- set the onscreen canvas positing to "destination-over" (new drawings are drawn behind the existing blurred brush-image
- drawImage the source image to the onscreen canvas (the blurred brush-image remains and the source image is drawn behind that brush-image)
Here's how that looks in code:
(The blur effect is done using quasimondo's nice blurring algorithm: http://jsfiddle/m1erickson/baDLp/)
// At this point, the temp canvas contains
// only the users brush strokes
// draw the image "clipped" into those brush strokes
// using positing == "source-in"
tempCtx.save();
tempCtx.globalCompositeOperation="source-in";
tempCtx.drawImage(img,0,0);
tempCtx.restore();
// blur the brush-image on the temp canvas
boxBlurCanvasRGBA("tempCanvas",0,0,tempCanvas.width,tempCanvas.height,4,0);
// clear the onscreen canvas
ctx.save();
ctx.clearRect(0,0,canvas.width,canvas.height);
// draw the brush-image from the temp canvas to the onscreen canvas
ctx.drawImage(tempCanvas,0,0);
// use positing == "destination-over"
// to draw the source image *behind* the blurred brush-image
ctx.globalCompositeOperation="destination-over";
ctx.drawImage(img,0,0);
ctx.restore();
Source Image:
Offscreen canvas after drawing the image only on the stroked area and then blurring:
The onscreen canvas with the blurred area merged into the source image:
本文标签: javascriptNeeded canvas blurring toolStack Overflow
版权声明:本文标题:javascript - Needed canvas blurring tool - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741645629a2390168.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论