Recent

Author Topic: Random SnowFlakes:  (Read 1121 times)

Boleeman

  • Hero Member
  • *****
  • Posts: 721
Random SnowFlakes:
« on: November 30, 2024, 08:14:46 am »
Hi All.

I have found a nice random Javascript snowflake generator that uses svg elements to generate random snowflake curves.

It is in a html file and is only one line long.

Code: Pascal  [Select][+][-]
  1. <canvas id=b><svg onload='with(Math)for(I=Y=0;Y++<70;I=random()-1.9&I+1)for(i=12;i--;C=X=>b.getContext`2d`.fillRect(70+X,s*j+c*Y+70,1,1)||X,C(-C((c=cos(a=PI/3*i))*j-(s=sin(a))*Y)))j=i>5&&I'>

I tried to convert to Lazarus using a TBgrabmp, but all I see is dots.

Is it possible to use svg in TBgrabmp to draw the random snowflakes correctly?

Can't seem to work out how to render the curve correctly so that it looks like a snowflake.
Must have something to do with the svg graphics ?
« Last Edit: December 02, 2024, 12:24:41 pm by Boleeman »

Boleeman

  • Hero Member
  • *****
  • Posts: 721
Re: Random SnowFlakes: Javascript conversion to Lazarus needed
« Reply #1 on: November 30, 2024, 08:37:53 am »
Somehow need to work out how to render svg in Lazarus ?

Dzandaa

  • Sr. Member
  • ****
  • Posts: 390
  • From C# to Lazarus
Re: Random SnowFlakes: Better Javascript conversion to Lazarus needed !
« Reply #2 on: November 30, 2024, 11:25:02 am »
Hi
@Boleeman:

In BGRAControls, there is a TBCSVGViewer

Create your SVG code in a string and put it in TBCSVGViewer.SVGString


B->

« Last Edit: November 30, 2024, 12:12:57 pm by Dzandaa »
Regards,
Dzandaa

Roland57

  • Sr. Member
  • ****
  • Posts: 475
    • msegui.net
Re: Random SnowFlakes: Better Javascript conversion to Lazarus needed !
« Reply #3 on: December 01, 2024, 04:39:47 pm »
Hi. I don't know the answer to your question, but here are my snowflakes using Cairo.  :)

https://forum.lazarus.freepascal.org/index.php/topic,57657.msg428979.html#msg428979
My projects are on Gitlab and on Codeberg.

Boleeman

  • Hero Member
  • *****
  • Posts: 721
Re: Random SnowFlakes: Better Javascript conversion to Lazarus needed !
« Reply #4 on: December 02, 2024, 05:08:57 am »
Sorry about the late reply: Been a bit crook lately with a bug.

Thanks Dzandaa and Roland57 for your replies about snowflakes.

Roland57: Compiled your Cairo Snowflake Lazarus source after downloading GTK from
http://downloads.sourceforge.net/gladewin32/gtk-2.12.9-win32-2.exe
I tried installing GTK3 runtime but was getting an error after compiling the Cairo snowflake code..

Also remembered seeing a few interesting cpp and CSharp snowflake examples on Codeproject ages ago.
Went to the Codeproject site yesterday and it looks like it has gone by way of the DODO .... Shutdown.

I compiled a CSharp Xmas Snowflakes code, but the flakes seem quite ordinary.
There was another Codeproject example that had some intricate random snowflakes but I can't find where I put it.

« Last Edit: December 02, 2024, 06:43:16 am by Boleeman »

Laksen

  • Hero Member
  • *****
  • Posts: 785
    • J-Software
Re: Random SnowFlakes: Better Javascript conversion to Lazarus needed !
« Reply #5 on: December 02, 2024, 07:17:25 am »
That's some well obfuscated code...

I think, minus the antialiasing and stuff this gets close
Code: Pascal  [Select][+][-]
  1. procedure TForm1.GenerateSnowflake(BgraBitmap: TBGRABitmap);
  2. var
  3.   Y, i, ii: integer;
  4.   j: integer;
  5.   a, c, s: double;
  6.  
  7.   function CC(X: double): double;
  8.   var
  9.     lx, ly: Int64;
  10.   begin
  11.     lx:=round(70+x);
  12.     ly:=round(s*j+c*y+70);
  13.     BgraBitmap.FillRect(lx, ly,lx+1, ly+1, BGRA(0, 0, 0), dmSet);
  14.     result:=X;
  15.   end;
  16. begin
  17.   I := 0;
  18.   for Y := 0 to 69 do
  19.   begin
  20.     for ii := 12 downto 0 do
  21.     begin
  22.       j := ifthen(ii > 5,I,0);
  23.  
  24.       a := PI/3*ii;
  25.       c := Cos(a);
  26.       s := Sin(a);
  27.       CC(-CC(c*j-s*Y));
  28.     end;
  29.  
  30.     I:=(trunc(random()-1.9) and (I+1));
  31.   end;
  32. end;
  33.  

Boleeman

  • Hero Member
  • *****
  • Posts: 721
Re: Random SnowFlakes: Better Javascript conversion to Lazarus needed !
« Reply #6 on: December 02, 2024, 08:46:51 am »
Laksen, that's fantastic.

Got the thickness working and Multiple Flakes.

I wonder if we could possibly smooth the flakes a bit ?

Made a small update: Realized that some curves were not that random, so put Randomize in the form's OnCreate.
« Last Edit: December 02, 2024, 09:19:31 am by Boleeman »

Boleeman

  • Hero Member
  • *****
  • Posts: 721
Re: Random SnowFlakes: Multiple Flakes but Smoothing Required
« Reply #7 on: December 02, 2024, 09:05:23 am »
I tried applying a Blur filter from TBgrabmp. A bit slower on the rendering now.
Would be nice if we could make the lines SVG.

What else could we possibly try to make the lines look smoother?
« Last Edit: December 02, 2024, 09:22:37 am by Boleeman »

Thaddy

  • Hero Member
  • *****
  • Posts: 16174
  • Censorship about opinions does not belong here.
Re: Random SnowFlakes: Multiple Flakes but Smoothing Required
« Reply #8 on: December 02, 2024, 10:37:20 am »
I had to invert the colours to obtain snowflakes....
If I smell bad code it usually is bad code and that includes my own code.

Boleeman

  • Hero Member
  • *****
  • Posts: 721
Re: Random SnowFlakes: Multiple Flakes but Smoothing Required
« Reply #9 on: December 02, 2024, 11:26:29 am »
Great idea Thaddy.

White Snowflakes on a black background !

Boleeman

  • Hero Member
  • *****
  • Posts: 721
Re: Random SnowFlakes: Multiple Flakes but Smoothing Required
« Reply #10 on: December 02, 2024, 12:24:18 pm »
Render to a TBgrabmp control.
« Last Edit: December 02, 2024, 12:28:04 pm by Boleeman »

Boleeman

  • Hero Member
  • *****
  • Posts: 721
Re: Random SnowFlakes:
« Reply #11 on: December 03, 2024, 09:01:13 am »
Sort of feeling like I need to start looking into fpVectorial to create some vector (svg) curves.

Javascript does svg well, but would like to look at Lazarus methods to do vector graphics.

zamtmn

  • Hero Member
  • *****
  • Posts: 640
Re: Random SnowFlakes:
« Reply #12 on: December 03, 2024, 09:23:12 am »
On topic of the upcoming holidays - it will be useful https://habr.com/ru/articles/246895/

Boleeman

  • Hero Member
  • *****
  • Posts: 721
Re: Random SnowFlakes:
« Reply #13 on: December 03, 2024, 09:07:14 pm »
Thanks zamtmn for that interesting Snowflake Maker information. And with Lazarus source.

I also came across a Snowflake Generator Webpage which does snowflakes as well.
The page saved OK so I can run it on my computer without connecting to the Net.
Might try to make something similar in Lazarus.

Late 2 png attachments: Compiled the program from zamtmn's "snowflaker" site suggestion and tested it out.
It uses Bezier type curves. Takes a bit of getting used to, but has real unique curve potential. COOL !

Thanks zamtmn.

« Last Edit: December 04, 2024, 01:34:41 pm by Boleeman »

 

TinyPortal © 2005-2018