Git Product home page Git Product logo

svg-with-classes's Introduction

SVG-with-Classes

After creating and using the 4D SVG component, I became aware of the need to build a new API much smaller, faster and closer to the SVG format and my use to manage the user interface.

The goal of this class is to reduce the complexity of code to create and manipulate svg images/documents.
This class will be augmented according to my needs but I strongly encouraged you to enrich this project through pull request. This can only benefit the 4D developer community.

For more details on properties and functions, see the documentation for the svg class

Enjoy the 4th dimension

Code sample

Using the 4D SVG component to create and manipulate SVG elements requires knowledge of many commands and becomes difficult to understand and maintain. Creating a simple SVG requires many lines of code:

// Create a new canvas
var $root : Text
$root:=SVG_New
	
// Create a "background" layer at the root level
var $background : Text 
$background:=SVG_New_group($root)
	
// Create a "foreground" layer, at the root level, and apply a translation
var $foreground : Text
$foreground:=SVG_New_group($root)
SVG_SET_TRANSFORM_TRANSLATE($foreground; 45; 45)
	
// Create a yellow square into the "foreground" layer
var $rect : Text
$rect:=SVG_New_rect($foreground; 2.5; 2.5; 20; 20)
SVG_SET_FILL_BRUSH($rect; "yellow")
SVG_SET_STROKE_BRUSH($rect; "yellow")
	
// Add, into the "background" layer, a blue circle without fill & with a border of 4 pixels
var $circle : Text
$circle:=SVG_New_circle($background; 100; 100; 50)
SVG_SET_FILL_BRUSH($circle; "none")
SVG_SET_STROKE_BRUSH($circle; "blue")
SVG_SET_STROKE_WIDTH($circle; 4)
	
// Create a red square into the "background" layer
$rect:=SVG_New_rect($background; 10; 10; 100; 100)
SVG_SET_FILL_BRUSH($rect; "red")
SVG_SET_STROKE_BRUSH($rect; "red")
	
// Show the result into the SVG viewer
SVGTool_SHOW_IN_VIEWER($root)
	
// Do not forget to release the memory !
SVG_CLEAR($root)

The svg class simplifies the creation and manipulation of the SVG elements thanks to a set of simple functions and some automatisms, and decrease the number of variables needed. Here is the equivalent code to get the same result (only 8 lines of easily understandable code versus 21 complicated lines with the component):

var $svg : cs.svg
	
// Create a new canvas
$svg:=cs.svg.new()
	
// Create a "background" & '"foreground" group & apply a translation to the last one
// [Layers are automatically created at the root level]
$svg.layer("background"; "foreground").translate(45; 45)
	
// Create a yellow square & memorize its reference as "original"
// [The object is automatically added to the latest created/used "container" ("foreground")]
$svg.square(20).position(2.5; 2.5).color("yellow").push("original")
	
// Set "background" layer for the next operations
If ($svg.with("background"))
	
	// Add, a blue circle without fill & with a border of 4 pixels
	$svg.circle(50).color("blue").position(100; 100).fill(False).stroke(4)
		
	// Clone the "original" square, colore it red, change its dimensions
	$svg.clone("original").color("red").position(10; 10).size(100; 100)
		
End if 
	
// Show the result into the SVG viewer
// [The memory is automatically freed]
$svg.preview()

The svg tree created:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" font-family="'lucida grande','segoe UI',sans-serif" font-size="12" preserveAspectRatio="none" shape-rendering="crispEdges" stroke="black" text-rendering="geometricPrecision" viewport-fill="none" xmlns:xlink="http://www.w3.org/1999/xlink">
  <g id="background">
    <circle cx="0" cy="0" fill="none" r="50" stroke="blue" stroke-width="4" transform="translate(100,100)"/>
    <rect fill="yellow" height="100" stroke="yellow" width="100" x="10" y="10"/>
  </g>
  <g id="foreground" transform="translate(45,45)">
    <rect fill="blue" height="20" stroke="blue" width="20" x="2.5" y="2.5"/>
  </g>
</svg>

The result image:

svg-with-classes's People

Contributors

vdelachaux avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

kirkbrooks

svg-with-classes's Issues

How to add elliptical arc curve commands

Hi Vincent,

I am trying to create a routine that will create 12 (or more) segments in a circle. I've attached an image from this page I am studying as an example: [https://observablehq.com/@haakenlid/svg-circle]

As far as I can tell, the only missing functions in the SVG class are "A" and "a".

All the other letter functions are expecting a collection of x,y coordinates. Elliptical arc curves require many more parameters. How should I extend the SVG class to accommodate these parameters:

A | a: (rx; ry; x-axis-rotation; large-arc-flag; sweep-flag; x; y)

Appreciate your help,
John...

segments

Donut Chart

Hi Vincent,

Enjoying the extension of the SVG class. I love how easy it is to create a pie chart. But I have not been able to get the donut from it. I get very very strange results!

Anyway this would require both an inner and outer radius (I guess the differences between the radius is the thickness), a start angle and an end angle. Like this example I mentioned previously:

https://observablehq.com/@haakenlid/svg-circle

Could you show how to use the SVg class to create a donut chart?

Appreciate,
John...

Compiler error with 4D 20.4

Hello,

I tried to use some of your classes, mainly xml and svg.
When it comes to compile with v 20.4 I've an error with '...', as above.
Function newRef($root : Text; $nameSpace : Text; ... : Text) : cs.xml
Seems to be okay with R6.
I don't know what it is, I mean '...', could you light me up?

Merci !

Line Patterns

Hi Vincent,

I'm trying to replace (using your SVG Classes):

"SVG_DashArray_Pattern($axisStyle_Mc_Asc; $line_svgRef)"

Is this possible? If so, may I please have an example?

Thanks,
John...

This.getTextHeight XML Error

Hi Vincent,

I am testing how to place text centered (horizontally/vertically) at an exact point in a circle. I am recreating code to use the svg class. Not sure if I should use "This.textArea" or "This.text"?

Offending code:

$oFont:={font: "'Monaco'"; size: 512}
$height:=This.getTextHeight("JJF"; $oFont)

image

image

Am I doing something wrong? Note: it does actually create the text after the errors.

I'm setting things up for this line:

This.textArea(This.oW.canvas.testString).position($origX; $origY).fontSize(512).alignment(Align center)

Appreciate,
John...

Missing Viewbox & Dimensions

Hi VIncent,

It looks like we are missing the equivalent to "SVG_SET_VIEWBOX" and "SVG_SET_DIMENSIONS".

I have scanned through the list of properties and cannot see it?

Can they easily be added?

Thanks,
John...

Are .size() & .with() missing functions?

Hi Vincent,

Does the svg class library have to be in v19r... Or can it work in 4D v18r6?

I ask because in v18r6 (presently Mac 15.6) the .size() and .with() functions do not appear to exist.

Appreciate,
John...

How do I embed multiple svg images (using svg & donut classes)

Hi Vincent,

I hope I have explained this clearly.

App: SVG-with-Classes
Classes; svg; chart
Method: Code Sample

Note: I will rename the “chart” class to “donut” class as it’s a particular kind of chart component the way I will be using it.

I’m recreating a complex drawing of different svg objects placed at various positions. Let’s say we have a form which has a picture variable. The picture variable corresponds to a page (could be portrait or landscape). The page is really just a picture variable that will contain many svg images/objects within it.

A chart consists of various components like circles and lines and “donuts” and other symbols. So, for example, I need to use a donut as part of a parent chart which is placed in an absolute location x/y on a page (svg document -> picture). The other svg images will be placed in different locations on the page.

A page might consist of objects like:

Single chart (including 1-4 donuts at various radii)
Info table (text columns in svg)
Grid (each cell will display an svg symbol)
Ruler/Slider with svg symbols built using svg
… and so on

Each are distinct svg images/objects embedded into an overall svg/picture variable. Of course, they will render on a form so will need to manage within a Form object.

Using the svg, donut and other classes (to be created) how do I add these to a single svg object which is then passed back into a picture object for display?

I’ve been working this way using the SVG Component for many years. I'm not sure what is the optimal approach using the svg class. It’s a whole different mindset.

I’m sure this is easy, and it’s in the docs, but I haven't been able to “see” it yet. And thus my efforts have failed.

How would you approach embedding multiple svg images?

Thanks,
John...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.