Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SVG!
{
"message": "Hello world!"
}
%dw 2.0
import * from svg
ns svg http://www.w3.org/2000/svg
var colors = ['#c00400', '#8db1ff', '#333333', '#f7d701', '#f8f6f4']
//['#f7bd99', '#01ab91', '#f05f48', '#703c4a', '#b29946', '#462c45']
fun randomColor() = colors[floor(random() * sizeOf(colors))]
fun circles() = 1 to 10 map circle({
x: random() * 1000,
y: random() * 650,
radius: 10 + 100 * random(),
fill: randomColor()
})
fun renderLogo() = {
svg#svg @(width: '100%', height: '100%', viewBox: '0 0 1000 1000'): {
(rect({x:-5000,y:0,width:10000,height:1000, fill: '#d2c4b7'})),
svg#g @(class: 'planets'): {
(circles())
},
(text({x: 480, y: 300, text: 'APILEDxxx'}))
}
}
// https://dw-svg-test.apiled.io/
import * from dw::http::Server
import * from dw::http::Client
import * from logo
fun renderSVG(x) = write(x, 'text/xml', {
encoding: "UTF-8",
onInvalidChar: "ignore",
inlineCloseOn: "none",
writeDeclaration: false
}) as Binary
---
api({
host: "0.0.0.0",
port: 8080
}, {
"/": {
GET: (request) -> {
body: renderSVG(renderLogo()),
headers: {
'Content-Type': 'image/svg+xml',
}
}
}
})
%dw 2.0
ns svg http://www.w3.org/2000/svg
type CircleOptions = {|
x: Number,
y: Number,
radius?: Number,
fill?: String
|}
fun circle(opts: CircleOptions) =
svg#circle @(fill: opts.fill default '#000000', cx: opts.x, cy: opts.y, r: opts.radius default 7): null
type RectOptions = {|
x: Number,
y: Number,
width: Number,
height: Number,
fill?: String
|}
type TextOptions = {|
x: Number,
y: Number,
text: String
|}
fun text(opts: TextOptions) =
svg#g @(
stroke: "none",
"stroke-width": "1",
fill: "none",
"fill-rule": "evenodd",
"font-size": "72",
"font-family": "Phosphate-Inline, Phosphate",
"font-weight": "normal"
): {
svg#text @(
fill: "#111213"
): {
svg#tspan @(
x: opts.x,
y: opts.y
): opts.text
}
}
fun rect(opts: RectOptions) =
svg#rect @(fill: opts.fill default '#FFFFFF', x: opts.x, y: opts.y, width: opts.width, height: opts.height): null
%dw 2.0
output application/xml
import * from logo
---
renderLogo()
{
"project": {
"name": "SVG!",
"mainScript": "scripts/main.dwl",
"apiled": {
"deploymentId": "3761031c-c4f0-42fc-8ad2-af8da24ae460",
"domain": "dw-svg-test"
}
},
"meta": {
"projectFileVersion": "0.1.0",
"creationDate": "12/05/2017"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.