ARRAYTO_SVG - James D Jarvis - 11-15-2023
being able to load SVGs into memory gives QB64 Phoenix Edition programs a very powerful vector drawing tool.
This in effect gives us a better draw command.
Here's a simple example that takes a two dimensional array of points and converts it to an SVG to be rendered.
Code: (Select All)
'arrayto_svg v0.1
'by James D. Jarvis 11/15/2023, QB64 3.9.0 or higher required
'Convert a two-dimensional numerical array into an SVG and draw that SVG on the screen
'
'made possible due to the SVG example in the QB64 Phoenix Edition WIKI example for _LOADIMAGE
'$dynamic
$Resize:Smooth
Dim As String svg
Dim img As Long
Dim a(3, 2), b(4, 2)
Screen _NewImage(1000, 500, 32)
a(1, 1) = 100: a(1, 2) = 100
a(2, 1) = 100: a(2, 2) = 200
a(3, 1) = 200: a(3, 2) = 200
svg = arrayto_svg$(a(), 20, "#EEEEEE", "#557733") 'convert the array a() into a closed polygon in an SVG
img = _LoadImage(svg, 32, "memory") 'load that SVG into the image
_PutImage , img 'draw the image
_FreeImage img 'free the image
b(1, 1) = 300: b(1, 2) = 200
b(2, 1) = 600: b(2, 2) = 200
b(3, 1) = 600: b(3, 2) = 300
b(4, 1) = 300: b(4, 2) = 300
svg = arrayto_svg$(b(), 5, "#770000", "#FFCCCC") 'convert the array a() into a closed polygon in an SVG
img = _LoadImage(svg, 32, "memory") 'load that SVG into the image
_PutImage , img 'draw the image
_FreeImage img 'free the image
Function arrayto_svg$ (aop(), swid, sklr As String, fklr As String)
'convert a two dimensional numerical array aop(p,2) to a closed polygon to draw as an SVG
Dim nsvg As String
maxp = UBound(aop)
nsvg = "<?xml version='1.0' encoding='utf-8'?>"
nsvg = nsvg + "<!-- Generator: QB64_SVG, SVG Export . SVG Version: 6.00 Build 0) -->"
nsvg = nsvg + "<svg version='1.2' baseProfile='tiny' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'"
nsvg = nsvg + " x='0px' y='0px' viewBox='0 0 1000 500' overflow='visible' xmlpace='preserve'>"
nsvg = nsvg + "<g>"
nsvg = nsvg + "<path fill='" + fklr + "' stroke='" + sklr + "' stroke-width='" + _Trim$(Str$(swid)) + "' stroke-miterlimit='10' d='M"
For p = 1 To maxp
nsvg = nsvg + Str$(aop(p, 1)) + "," + Str$(aop(p, 2)) + ","
Next p
nsvg = nsvg + Str$(aop(1, 1)) + "," + Str$(aop(1, 2))
nsvg = nsvg + "'/>"
nsvg = nsvg + "</g>"
nsvg = nsvg + "</svg>"
arrayto_svg$ = nsvg
End Function
RE: ARRAYTO_SVG - bplus - 11-15-2023
Is there supposed to be an image to go with this?
RE: ARRAYTO_SVG - James D Jarvis - 11-15-2023
(11-15-2023, 07:15 PM)bplus Wrote: Is there supposed to be an image to go with this?
No. The image is generated from the arrays in the example.
RE: ARRAYTO_SVG - bplus - 11-15-2023
Then I am getting ... wait does this require v3.9+ ?
RE: ARRAYTO_SVG - James D Jarvis - 11-15-2023
(11-15-2023, 09:44 PM)bplus Wrote: Then I am getting ... wait does this require v3.9+ ?
Yes. 3.9 plus is required.
|