Recent

Author Topic: BGRABitmap center and scale question  (Read 10656 times)

lainz

  • Hero Member
  • *****
  • Posts: 3709
  • Leandro Diaz
Re: BGRABitmap center and scale question
« Reply #15 on: September 03, 2016, 12:14:10 pm »
I'm looking at your code.

That's because you have the bitmap beign deleted on ListBox1Click. As I said you need to put the drawing in the event, is better, or not delete the bitmap in the listboxclick event.

Do this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1Click(Sender: TObject);
  2. begin
  3.   DrawStuff := True;
  4.   BGRAVirtualScreen1.DiscardBitmap;
  5. end;

Code: Pascal  [Select][+][-]
  1. procedure TForm1.BGRAVirtualScreen1Redraw(Sender: TObject; Bitmap: TBGRABitmap);
  2. var
  3.   i: integer;
  4.   boxX, boxY : integer;
  5. begin
  6.   if not DrawStuff then
  7.     exit;
  8.  
  9.   boxX := (ClientWidth) div 4;
  10.   boxY := (ClientHeight) div 4;
  11.   ctx := Bitmap.Canvas2D;
  12.   ctx.save();
  13.   //ctx.translate(boxX, boxY);
  14.   ctx.antialiasing := True;
  15.   ctx.scale(1, 1);
  16.   ctx.pixelCenteredCoordinates := False;
  17.   ctx.lineWidth:=3;
  18.   ctx.strokeStyle(clBlack);
  19.  
  20.   ctx.beginPath;
  21.   ctx.moveTo(10, 10);
  22.   ctx.lineTo(500, 10);
  23.   ctx.moveTo(500, 10);
  24.   ctx.lineTo(500, 200);
  25.   ctx.moveTo(500, 200);
  26.   ctx.lineTo(10, 200);
  27.   ctx.moveTo(10, 200);
  28.   ctx.lineTo(10, 10);
  29.   ctx.stroke();
  30. end;    

You will notice that this always draw. Maybe you can add a boolean like DrawStuff. Set DrawStuff := False OnCreate.

scons

  • Full Member
  • ***
  • Posts: 139
Re: BGRABitmap center and scale question
« Reply #16 on: September 04, 2016, 10:30:11 am »

That's because you have the bitmap beign deleted on ListBox1Click. As I said you need to put the drawing in the event, is better, or not delete the bitmap in the listboxclick event.


You will notice that this always draw. Maybe you can add a boolean like DrawStuff. Set DrawStuff := False OnCreate.

Lainz thanks for your input, I will test your improvements
Windows 10-64bit Lazarus 1.8.4 + FPC 3.0.4

scons

  • Full Member
  • ***
  • Posts: 139
Re: BGRABitmap center and scale question
« Reply #17 on: September 04, 2016, 10:42:05 am »
I ran some quick tests, I noticed there is an offset in the rotated text part of a loop. The rotated items (vertical ones) or not lined up in the same way as the ones that are not rotated (horizontal ones). There is no difference in code (to me) in the placement of the text. Strange thing is that when you enter the loop for a second time, the offset is bigger. What am I coding wrong here ?

This is the loop for the circles and measuring:
Code: Pascal  [Select][+][-]
  1.                 Val(sX, holX, error);
  2.                 Val(sY, holY1, error);
  3.                 holY := varY1 - holY1;
  4.                 Val(sline.ValueFromIndex[3], radX, error);
  5.                 radX1 := radX / 2;
  6.  
  7.                 //place circles
  8.                 ctx.lineWidth := 3;
  9.                 ctx.strokeStyle(clBlack);
  10.                 ctx.fillStyle(clWhite);
  11.                 ctx.beginPath;
  12.                 ctx.circle(holX, holY, radX1);
  13.                 ctx.fill();
  14.                 ctx.stroke();
  15.                 //measure X (rotated text)
  16.                 ctx.fontEmHeight := 15;
  17.                 ctx.lineWidth := 1;
  18.                 ctx.strokeStyle(clRed);
  19.                 ctx.beginPath;
  20.                 ctx.moveTo(holX, holY -5);
  21.                 ctx.lineTo(holX, varY1 + 80);
  22.                 ctx.stroke();
  23.                 ctx.save();
  24.                 ctx.rotate(degtorad(-90));
  25.                 ctx.fillStyle(clRed);
  26.                 varM := single(holX);
  27.                 ctx.text( (diamsym + FloatToStrF(radX, ffFixed, 10, 0) + '  ' + FloatToStrF(varM, ffFixed,10,2)), -varY1 -80,holX);
  28.                 ctx.fill();
  29.                 ctx.restore();
  30.  
  31.                 //measure Y (horizontal text)
  32.                 ctx.fontEmHeight := 15;
  33.                 ctx.lineWidth := 1;
  34.                 ctx.strokeStyle(clRed);
  35.                 ctx.beginPath;
  36.                 ctx.moveTo(-80, holY);
  37.                 ctx.lineTo(holX +5, holY);
  38.                 ctx.stroke();
  39.                 ctx.fillStyle(clRed);
  40.                 varM := single(holY1);
  41.                 ctx.text( (diamsym + FloatToStrF(radX, ffFixed, 10, 0) + '  ' + FloatToStrF(varM, ffFixed,10,2)), -80,holY-2);
  42.                 ctx.fill();
  43.  
  44.  
Windows 10-64bit Lazarus 1.8.4 + FPC 3.0.4

circular

  • Hero Member
  • *****
  • Posts: 3446
    • Personal webpage
Re: BGRABitmap center and scale question
« Reply #18 on: September 04, 2016, 05:22:15 pm »
I would suggest to do save(), translate(x of text, y of text), rotate(...), text('dfgdsfgsdfg', 0,0), restore()

so that the text coordinates are not transformed by the rotation.
Conscience is the debugger of the mind

scons

  • Full Member
  • ***
  • Posts: 139
Re: BGRABitmap center and scale question
« Reply #19 on: September 30, 2016, 03:24:15 pm »

That's because you have the bitmap beign deleted on ListBox1Click. As I said you need to put the drawing in the event, is better, or not delete the bitmap in the listboxclick event.


You will notice that this always draw. Maybe you can add a boolean like DrawStuff. Set DrawStuff := False OnCreate.

Lainz thanks for your input, I will test your improvements

I had some "spare" time and implemented your suggestions, it works a lot better now, thanks !
Windows 10-64bit Lazarus 1.8.4 + FPC 3.0.4

scons

  • Full Member
  • ***
  • Posts: 139
Re: BGRABitmap center and scale question
« Reply #20 on: September 30, 2016, 03:29:47 pm »
I would suggest to do save(), translate(x of text, y of text), rotate(...), text('dfgdsfgsdfg', 0,0), restore()

so that the text coordinates are not transformed by the rotation.

If I do this:

Code: Pascal  [Select][+][-]
  1. ctx.save();
  2. ctx.translate(holX,varY1 +80);
  3. ctx.rotate(degtorad(-90));
  4. ctx.fillStyle(clRed);
  5. varM := single(holX);
  6. ctx.text( (diamsym + FloatToStrF(radX, ffFixed, 10, 0) + '  ' + FloatToStrF(varM, ffFixed,10,2)),0,0);
  7. ctx.fill();
  8. ctx.restore();

in comparison to this:

Code: Pascal  [Select][+][-]
  1. ctx.save();
  2. ctx.rotate(degtorad(-90));
  3. ctx.fillStyle(clRed);
  4. varM := single(holX);
  5. ctx.text( (diamsym + FloatToStrF(radX, ffFixed, 10, 0) + '  ' + FloatToStrF(varM, ffFixed,10,2)), -varY1 -80,holX);
  6. ctx.fill();
  7. ctx.restore();

There is still an offset. Do you have any other suggestions ?
Windows 10-64bit Lazarus 1.8.4 + FPC 3.0.4

circular

  • Hero Member
  • *****
  • Posts: 3446
    • Personal webpage
Re: BGRABitmap center and scale question
« Reply #21 on: October 01, 2016, 05:56:56 pm »
Are you comfortable posting the whole project or a test project with just what's needed?
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018