krzysztofzablocki / LineDrawing

Beatiful and fast smooth line drawing algorithm for iOS - as seen in Foldify.
http://twitter.com/merowing_
Other
1.29k stars 213 forks source link

How can i add background image? #7

Closed yatanadam closed 11 years ago

yatanadam commented 11 years ago

Hi again. How can i add background image to rendertexture ? Thanks.

krzysztofzablocki commented 11 years ago

Either render sprite to renderTexture at start or have a background image behind the renderTexture and make the renderTexture transparent in parts that you did not draw.

tolgatanriverdi commented 11 years ago

Hi Thanks for the answer I have the same problem . I want to draw on an image which I put as background however let me explained what happened when I followed your suggestions

1.If I put render the background image into renderTexture either only my background picture seemed and my drawings did not or my drawings seemed but not my background picture .I've tried to change their z-order and somehow it worked but then I figured out that when I erase the lines that I draw it erases the background image too 2.I've added the background image like this

            backgroundImage = [CCSprite spriteWithCGImage:bgNativeImage.CGImage key:@"backgroundImage"];
            [self calculateBgImageSize];
            backgroundImage.opacity = 150;
            [self addChild:backgroundImage z:1];

only this time I could be able to draw and erase lines on top of background image but this time my image seems transparent and it iritates the user Can you suggest me a spesific way to handle this ? I thought on this a lot and it seems to me that only way to draw and erase the lines on top of an image is using layers such as photoshop

tolgatanriverdi commented 11 years ago

I've made something like this according to kryzstofz suggestion

    if (self.bgNativeImage) {
        if (!backgroundImage) {
            backgroundImage = [CCSprite spriteWithCGImage:bgNativeImage.CGImage key:@"backgroundImage"];
            [self calculateBgImageSize];
            [backgroundImage setBlendFunc:(ccBlendFunc){GL_ONE, GL_ZERO}];
            [backgroundLayer addChild:backgroundImage];
            //backgroundImage.opacity = 150;
            //[self addChild:backgroundImage z:1];
        }
        //[backgroundImage visit];

    } 

[renderTexture begin];

NSMutableArray *smoothedPoints = [self calculateSmoothLinePoints];
if (smoothedPoints) {
    [self drawLines:smoothedPoints withColor:color];
}

if (self.bgNativeImage) {
    [backgroundLayer setZOrder:1];
    [backgroundLayer visit];
}

[renderTexture end];

now I can draw on image without any problem however when I try to erase the lines that I already draw.(with below algorithm) it also erases the background image

ccColor4F color = {0, 0, 0, 1};
if (!isPencilActive) {
    ccColor4F eraserColor = {1,1,1,1};
    color = eraserColor;
} else {
    color = drawingColor;
}

Do you have any idea about it? Thanks Tolga