tags:

views:

1064

answers:

2

I had an answer before and it was very helpful, thus I modified my code to make it work,however, I'm still in square-1

//load libs
import flash.net.*;
import flash.geom.Matrix;
import flash.display.*;
import flash.events.*;
import com.adobe.images.JPGEncoder;

function myLoader() {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, sketch);

    var request:URLRequest = new URLRequest("http://www.sergiorodriguez.org/images/2008_5_FXD_VividBlack.jpg");
    loader.load(request);
}

//action for mouse 
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveCursor);
Mouse.hide();

function moveCursor(event:MouseEvent):void{
 pencil.x = event.stageX;
 pencil.y = event.stageY;
}


var canvas_mc;
 canvas_mc = new MovieClip()

addChildAt(canvas_mc,0);
canvas_mc.swapDepths

//draw area to sketch
function sketch(e:Event):void{
 //load bitmap and draw it to memory
 var myBitmapData;
  myBitmapData = new BitmapData(500, 500);
  myBitmapData.draw(e);

 //define matrix
 var matrix;
  matrix = new flash.geom.Matrix();

 //start canvas
 canvas_mc.graphics.beginBitmapFill(myBitmapData,matrix, true, true);
 canvas_mc.graphics.drawRect(0, 0, 500, 500);
 canvas_mc.graphics.endFill();

 //listening  events
 canvas_mc.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
 canvas_mc.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);
 canvas_mc.addEventListener(MouseEvent.MOUSE_MOVE, makeLine); 
}  

//mouse draws on press
function startDrawing(event:MouseEvent):void{ 
 canvas_mc.graphics.lineStyle(1, 0, 1);
 canvas_mc.graphics.moveTo(mouseX, mouseY);
 canvas_mc.addEventListener(MouseEvent.MOUSE_MOVE, makeLine);
}

//mouse stops drawing on realese
function stopDrawing(event:MouseEvent):void{
 canvas_mc.removeEventListener(MouseEvent.MOUSE_MOVE, makeLine);
}

//creates lines
function makeLine(event:MouseEvent):void{
 canvas_mc.graphics.lineTo(mouseX, mouseY); 
}

//call function
myLoader()

//start to save onto server
var serverPath:String = "";
function createJPG(m:MovieClip, q:Number, fileName:String){
 var jpgSource:BitmapData = new BitmapData (m.width, m.height);
 jpgSource.draw(m);
 var jpgEncoder:JPGEncoder = new JPGEncoder(q);
 var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
 var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
 var jpgURLRequest:URLRequest = new URLRequest ( serverPath+"jpg_encoder_download.php?name=" + fileName + ".jpg");  
 jpgURLRequest.requestHeaders.push(header);    
 jpgURLRequest.method = URLRequestMethod.POST;    
 jpgURLRequest.data = jpgStream;

 var jpgURLLoader:URLLoader = new URLLoader(); 
 //jpgURLLoader.load(jpgURLRequest);  
 navigateToURL(jpgURLRequest, "_blank");
}

save_btn.addEventListener(MouseEvent.CLICK, saveBtnPress);
save_btn.addEventListener(MouseEvent.ROLL_OVER, saveBtnOver);
save_btn.addEventListener(MouseEvent.ROLL_OUT, saveBtnOut);

function saveBtnPress(e:Event):void{ 
createJPG(canvas_mc, 90, "sketch");
}

function saveBtnOver(e:Event):void{ 
 Mouse.show();
 pencil.visible = false;
}

function saveBtnOut(e:Event):void{ 
 Mouse.hide();
 pencil.visible = true;
}
+1  A: 

I'm not sure what the question is, but I assume that you're trying to paint on the image loaded by the myLoader() function, and that you don't see the image. In that case, the problem is that you didn't add the Loader instance to the main sprite as a child. Here's the fix:

function myLoader() {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, sketch);

    var request:URLRequest = new URLRequest("http://www.sergiorodriguez.org/images/2008_5_FXD_VividBlack.jpg");
    loader.load(request);
    addChild(loader);
}

Explanation: the loader will eventually cointain the loaded image, but if you want it displayed, you need to display the loader itself.

David Hanak
thanks for the answer, here is what I'm trying to accomplish:I'm creating a drawing board on which I want to load an image on the background. I should be able to draw on top of the image.
Sergio Rodriguez
A: 

You can't draw the event to the Bitmap. You need to draw the loaded image

change your line:

myBitmapData.draw(e);

into

myBitmapData.draw(loader);

I suspect your code has a bunch of other problems, i can't really see you add the BitmapData to any Bitmap anywhere, so you won't ever see what's drawn.

Also, try to be a bit more specific in your questions, it's a lot easier to respond if we don't have to read the whole code through.

grapefrukt
thanks for you help, I worked. :)
Sergio Rodriguez