Jump to content
Sign in to follow this  
Flemming

TW3BufferedImage to draw scaled canvas

Recommended Posts

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; 

Share this post


Link to post
Share on other sites

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

 

 

Share this post


Link to post
Share on other sites

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; 

... 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×