-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from VOSID8/main
Added svg documentation from doodle-svg to doodle
- Loading branch information
Showing
12 changed files
with
311 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ laika.navigationOrder = [ | |
interact | ||
algebra | ||
effect | ||
development | ||
svg | ||
development | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Doodle SVG | ||
|
||
Doodle SVG draws Doodle pictures to SVG, both in the browser using [Scala JS](https://scala-js.org/) and to files on the JVM. | ||
|
||
## Usage | ||
|
||
Firstly, bring everything into scope | ||
|
||
```scala | ||
import doodle.svg._ | ||
``` | ||
|
||
Now what you can do depends on whether you are running in the browser or on the JVM. | ||
|
||
|
||
### Running in the Browser | ||
|
||
In the browser you can draw a picture to SVG. To do this, construct a `Frame` with the `id` of the DOM element where you'd like the picture drawn. | ||
|
||
For example, if you have the following element in your HTML | ||
|
||
``` html | ||
<div id="svg-root"></div> | ||
``` | ||
|
||
then you can create a `Frame` referring to it with | ||
|
||
``` scala mdoc:silent | ||
val frame = Frame("svg-root") | ||
``` | ||
|
||
Now suppose you have a picture called `thePicture`. You can draw it using the frame you just created like so | ||
|
||
``` scala | ||
thePicture.drawWithFrame(frame) | ||
``` | ||
|
||
The rendered SVG will appear where the element is positioned on your web page. | ||
|
||
|
||
## Running on the JVM | ||
|
||
On the JVM you can't draw SVG to the screen. Use the `java2d` backend for that instead. However you can write SVG output in the usual way. | ||
|
||
|
||
## Examples | ||
|
||
The source for these examples is [in the repository](https://github.com/creativescala/doodle-svg/tree/main/examples/src/main/scala). | ||
|
||
### Concentric Circles | ||
@:doodle("concentric-circles", "ConcentricCircles.draw") | ||
|
||
### Text Positioning | ||
@:doodle("text-positioning", "TextPositioning.draw") | ||
|
||
### Doodle Logo | ||
@:doodle("doodle-logo", "DoodleLogo.draw") | ||
|
||
### Pulsing Circle | ||
@:doodle("pulsing-circle", "PulsingCircle.draw") | ||
|
||
### Parametric Spiral | ||
@:doodle("parametric-spiral", "ParametricSpiral.draw") |
39 changes: 39 additions & 0 deletions
39
examples/js/src/main/scala/doodle/examples/ConcentricCircles.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2015 Creative Scala | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package doodle | ||
package svg | ||
|
||
import doodle.core._ | ||
import doodle.svg._ | ||
import doodle.syntax.all._ | ||
import scala.scalajs.js.annotation._ | ||
import cats.effect.unsafe.implicits.global | ||
|
||
@JSExportTopLevel("ConcentricCircles") | ||
object ConcentricCircles { | ||
def circles(count: Int): Picture[Unit] = | ||
if (count == 0) Picture.circle(20).fillColor(Color.hsl(0.degrees, 0.7, 0.6)) | ||
else | ||
Picture | ||
.circle(count.toDouble * 20.0) | ||
.fillColor(Color.hsl((count * 15).degrees, 0.7, 0.6)) | ||
.under(circles(count - 1)) | ||
|
||
@JSExport | ||
def draw(mount: String) = | ||
circles(7).drawWithFrame(Frame(mount)) | ||
} |
46 changes: 46 additions & 0 deletions
46
examples/js/src/main/scala/doodle/examples/DoodleLogo.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2015 Creative Scala | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package doodle | ||
package svg | ||
|
||
import doodle.core._ | ||
import doodle.core.font._ | ||
import doodle.svg._ | ||
import doodle.syntax.all._ | ||
import scala.scalajs.js.annotation._ | ||
import cats.effect.unsafe.implicits.global | ||
|
||
@JSExportTopLevel("DoodleLogo") | ||
object DoodleLogo { | ||
val font = | ||
Font.defaultSansSerif.size(FontSize.points(22)).weight(FontWeight.bold) | ||
val logo: Picture[Unit] = | ||
(0.to(10)) | ||
.map(i => | ||
Picture | ||
.text("Doodle SVG") | ||
.font(font) | ||
.fillColor(Color.hotpink.spin(10.degrees * i.toDouble)) | ||
.at(i * 3.0, i * -3.0) | ||
) | ||
.toList | ||
.allOn | ||
|
||
@JSExport | ||
def draw(mount: String) = | ||
logo.drawWithFrame(Frame(mount)) | ||
} |
44 changes: 44 additions & 0 deletions
44
examples/js/src/main/scala/doodle/examples/ParametricSpiral.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import doodle.core._ | ||
import doodle.svg._ | ||
import doodle.syntax.all._ | ||
import scala.scalajs.js.annotation._ | ||
import cats.effect.unsafe.implicits.global | ||
|
||
@JSExportTopLevel("ParametricSpiral") | ||
object ParametricSpiral { | ||
|
||
def parametricSpiral(angle: Angle): Point = | ||
Point((Math.exp(angle.toTurns) - 1) * 200, angle) | ||
|
||
def drawCurve( | ||
points: Int, | ||
marker: Point => Picture[Unit], | ||
curve: Angle => Point | ||
): Picture[Unit] = { | ||
// Angle.one is one complete turn. I.e. 360 degrees | ||
val turn = Angle.one / points.toDouble | ||
def loop(count: Int): Picture[Unit] = { | ||
count match { | ||
case 0 => | ||
val pt = curve(Angle.zero) | ||
marker(pt).at(pt) | ||
case n => | ||
val pt = curve(turn * count.toDouble) | ||
marker(pt).at(pt).on(loop(n - 1)) | ||
} | ||
} | ||
|
||
loop(points) | ||
} | ||
|
||
@JSExport | ||
def draw(id: String): Unit = { | ||
val marker = (point: Point) => | ||
Picture | ||
.circle(point.r * 0.125 + 7) | ||
.fillColor(Color.red.spin(point.angle / 4.0)) | ||
.noStroke | ||
|
||
drawCurve(20, marker, parametricSpiral _).drawWithFrame(Frame(id)) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
examples/js/src/main/scala/doodle/examples/PulsingCircle.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2015 Creative Scala | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package doodle | ||
package svg | ||
|
||
import doodle.core._ | ||
import doodle.svg._ | ||
import doodle.syntax.all._ | ||
import doodle.interact.syntax.all._ | ||
import scala.scalajs.js.annotation._ | ||
import cats.effect.unsafe.implicits.global | ||
|
||
@JSExportTopLevel("PulsingCircle") | ||
object PulsingCircle { | ||
def circle(count: Double): Picture[Unit] = | ||
Picture | ||
.circle(2 * count + 10) | ||
.fillColor(Color.hsl((count * 5).degrees, 0.7, 0.6)) | ||
|
||
@JSExport | ||
def draw(mount: String) = | ||
0.0 | ||
.upToIncluding(72.0) | ||
.map(c => circle(c)) | ||
.forSteps(120) | ||
.andThen(_ => 72.0.upToIncluding(0.0).map(c => circle(c)).forSteps(120)) | ||
.repeatForever | ||
.animate(Frame(mount).withSize(72 * 2 + 10, 72 * 2 + 10)) | ||
} |
43 changes: 43 additions & 0 deletions
43
examples/js/src/main/scala/doodle/examples/TextPositioning.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2015 Creative Scala | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package doodle | ||
package svg | ||
|
||
import doodle.core._ | ||
import doodle.core.font._ | ||
import doodle.svg._ | ||
import doodle.syntax.all._ | ||
import scala.scalajs.js.annotation._ | ||
import cats.effect.unsafe.implicits.global | ||
|
||
@JSExportTopLevel("TextPositioning") | ||
object TextPositioning { | ||
val font = Font.defaultSansSerif.size(FontSize.points(18)) | ||
def text(string: String): Picture[Unit] = | ||
Picture.text(string).font(font).fillColor(Color.hotpink) | ||
|
||
val textPositioning: Picture[Unit] = | ||
text("Above") | ||
.above( | ||
text("Center").on(Picture.circle(100).strokeWidth(3.0)) | ||
) | ||
.above(text("Below")) | ||
|
||
@JSExport | ||
def draw(mount: String) = | ||
textPositioning.drawWithFrame(Frame(mount)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "doodle", | ||
"version": "1.0.0", | ||
"description": "Copyright [Noel Welsh](http://noelwelsh.com).", | ||
"main": "tailwind.config.js", | ||
"directories": { | ||
"doc": "docs", | ||
"example": "examples" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
} |