Jump to content


Photo

TW3BufferedImage to draw scaled canvas


  • Please log in to reply
3 replies to this topic

#1 Flemming

Flemming
  • Members
  • 47 posts

Posted 22 October 2013 - 08:18 PM

Hi

I want to make a painting canvas with zoom in and out function. I have found that the TW3BufferedImage should be capable of doing so with DrawScaled, but i'm not able to get it working ?


 procedure TMainForm.HandleButtonClicked(Sender: TObject); 
 var 
   FCanvas: TW3Canvas; 
   FGradient: TW3CanvasGradient; 
 begin 
   FBuffer:=TW3BufferedImage.Create(FImage.Width,FImage.Height); 
  
   FGraph.Allocate(FImage.Width,FImage.Height); 
   FCanvas := TW3Canvas.Create(FGraph); 
   try 
     FBuffer.Canvas.BeginPath(); 
     FBuffer.Canvas.MoveToF(2,2); 
     FBuffer.Canvas.BezierCurveToF(300,400,300,500,200,100); 
     FBuffer.canvas.lineWidth := 5; 
     FBuffer.canvas.strokeStyle := 'blue'; 
     FBuffer.canvas.stroke(); 
  
     FBuffer.DrawScaled(FCanvas,0,0,0.5); 
     FImage.LoadFromUrl(FCanvas.toDataUrl('')); 
 //    FImage.LoadFromUrl(FBuffer.Canvas.toDataUrl('')); 
   finally 
     FCanvas.Free; 
     FBuffer.Free; 
   end; 
   FGraph.Release; 
 end; 


#2 Eric

Eric
  • Moderators
  • 96 posts

Posted 22 October 2013 - 08:30 PM

The idea of the buffered image is to work as a buffered render target, so you should draw in the OnRedraw event. DrawScaled will automatically invoke it when it hasn't been Prepared already (either because it was never drawn to or because you called the Invalidate method).

In the OnRedraw event you get the buffered image as Sender, and should draw to the Canvas.

So in your case, you would:
- create the buffer image somewhere in the form construction or initialization sequence
- register your drawing code in its OnRedraw
- in whatever will result in the image getting changed, call Invalidate (this won't draw anything, so you can call it as many times and as often as you want)
- in HandleButtonClicked, use DrawScaled on your target canvas, if the buffer was invalidated, your OnRedraw handler will be called automatically, if nothing was changed, OnRedraw won't be called



#3 Flemming

Flemming
  • Members
  • 47 posts

Posted 23 October 2013 - 03:39 PM

Sorry but i'm a little confused: The buffer draw's to a Canvas that get's loaded into the image ? But the OnRedraw's Sender is the bufferedimage ?

btw. I'm going to use a lot of direct pixel access and i'm not sure if bufferedimage is the right way to go for fast pixel access ?

  
  private 
    FBuffer : TW3BufferedImage; 
    FCanvas: TW3Canvas; 
    FImage: TW3Image; 
     
 ... 
  
 procedure TMainForm.BufferOnRedraw(Sender : TW3BufferedImage); 
 begin 
   FImage.LoadFromUrl(FCanvas.ToDataURL('')); 
 //  FImage.LoadFromUrl(Sender.Canvas.ToDataURL('')); 
 end; 
  
 procedure TMainForm.HandleButtonClicked(Sender: TObject); 
 begin 
   FBuffer.Canvas.BeginPath(); 
   FBuffer.Canvas.MoveToF(2,2); 
   FBuffer.Canvas.BezierCurveToF(300,400,300,500,200,100); 
   FBuffer.canvas.lineWidth := 5; 
   FBuffer.canvas.strokeStyle := 'blue'; 
   FBuffer.canvas.stroke(); 
   FBuffer.DrawScaled(FCanvas,0,0,0.5); 
 //  FBuffer.Invalidate; 
 end; 
  
 ... 
  


#4 Flemming

Flemming
  • Members
  • 47 posts

Posted 02 November 2013 - 02:41 PM

I'm stucked here, so i would really appreciate a working example on TW3BufferedImage !?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users