Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Immediate Mode in Doodle Canvas #168

Open
wants to merge 44 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
b46b037
Something
VOSID8 Jun 23, 2024
ac31aad
Formatting and undoing experimental changes
VOSID8 Jun 23, 2024
0deaf63
Edited name of the file
VOSID8 Jun 23, 2024
ed749d3
Implemeting dummy
VOSID8 Jun 23, 2024
6a95f8d
Removed junk code
VOSID8 Jun 25, 2024
739b907
Fixing Reified file
VOSID8 Jun 25, 2024
c52c5b6
Removing junk files
VOSID8 Jun 25, 2024
5418467
Removing junk files
VOSID8 Jun 25, 2024
9e05dcf
Changed type parameter to raster[A] to trait[A]
VOSID8 Jun 26, 2024
b22fabd
Changed type parameter to raster[A] to trait[A]
VOSID8 Jun 26, 2024
d882c6e
Removed junk file
VOSID8 Jun 26, 2024
d9c5745
Removed Raster from Image and added an experiment example
VOSID8 Jun 27, 2024
f4deb93
Removed Raster from Image and added an experiment example
VOSID8 Jun 27, 2024
879be92
Added Headers to 2 files
VOSID8 Jun 27, 2024
ce43de1
Adding 2nd parameter in example
VOSID8 Jun 27, 2024
7d53610
prePR formatted the code
VOSID8 Jun 27, 2024
4c4c4a6
Fix ScalaFix configuration
noelwelsh Jun 27, 2024
5b51b48
Fix raster syntax
noelwelsh Jun 27, 2024
300f5fa
Add raster syntax to JVM syntax
noelwelsh Jun 27, 2024
635298d
Organize imports
noelwelsh Jun 27, 2024
cab9c2f
Update workflows
noelwelsh Jun 27, 2024
57aa401
Merge pull request #1 from noelwelsh/canvasRaster
VOSID8 Jun 28, 2024
dada964
Changing Algrabra to something of combinator
VOSID8 Jun 30, 2024
5ab7c2c
Raster as constructor
VOSID8 Jul 1, 2024
eddca62
Added transformation to the Raster
VOSID8 Jul 3, 2024
a8be38b
Replacing Canvas Methods with Doodle's
VOSID8 Jul 10, 2024
4a1268a
Replacing Canvas Methods with Doodle's
VOSID8 Jul 10, 2024
8b9d0a3
Passing canvasContext as implicit
VOSID8 Jul 12, 2024
176c370
First working Immediate model
VOSID8 Jul 13, 2024
18c6a84
meh5
VOSID8 Jul 17, 2024
1fbc41f
Implemented few Immediate methods
VOSID8 Jul 18, 2024
826e35f
Added Methods for Immediate
VOSID8 Jul 24, 2024
52ddb09
fillColor to fill
VOSID8 Aug 2, 2024
acc8921
Methods added
VOSID8 Aug 7, 2024
7906eeb
Added text method
VOSID8 Aug 14, 2024
ad31840
Added dash Shapes and text
VOSID8 Aug 16, 2024
167b572
Added star
VOSID8 Aug 17, 2024
66ca336
Added canvasImmediate Landscape example and few fixes
VOSID8 Aug 17, 2024
ec3054a
Formatted the code
VOSID8 Aug 18, 2024
69b6150
GitWorkflowGenerate
VOSID8 Aug 18, 2024
30f91b4
Removed warnings
VOSID8 Aug 18, 2024
ced2f8e
Removed cmd /c from sbt file
VOSID8 Aug 18, 2024
24bc8c5
Few changes and example setup
VOSID8 Aug 20, 2024
37f2618
Fixed methods"
VOSID8 Aug 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ jobs:

- name: Make target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: mkdir -p reactor/js/target image/js/target unidocs/target js/target reactor/jvm/target core/js/target examples/js/target turtle/js/target core/jvm/target jvm/target interact/js/target interact/jvm/target java2d/target examples/jvm/target turtle/jvm/target image/jvm/target svg/jvm/target svg/js/target golden/target target canvas/target project/target
run: mkdir -p reactor/js/target image/js/target unidocs/target js/target reactor/jvm/target core/js/target examples/js/target turtle/js/target core/jvm/target jvm/target interact/js/target interact/jvm/target java2d/target examples/jvm/target target turtle/jvm/target image/jvm/target svg/jvm/target svg/js/target golden/target canvas/target project/target

- name: Compress target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: tar cf targets.tar reactor/js/target image/js/target unidocs/target js/target reactor/jvm/target core/js/target examples/js/target turtle/js/target core/jvm/target jvm/target interact/js/target interact/jvm/target java2d/target examples/jvm/target turtle/jvm/target image/jvm/target svg/jvm/target svg/js/target golden/target target canvas/target project/target
run: tar cf targets.tar reactor/js/target image/js/target unidocs/target js/target reactor/jvm/target core/js/target examples/js/target turtle/js/target core/jvm/target jvm/target interact/js/target interact/jvm/target java2d/target examples/jvm/target target turtle/jvm/target image/jvm/target svg/jvm/target svg/js/target golden/target canvas/target project/target

- name: Upload target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
Expand Down
1 change: 1 addition & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rules = [
OrganizeImports
]
OrganizeImports.removeUnused = false
OrganizeImports.coalesceToWildcardImportThreshold = 5
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ lazy val docs =
val dest1 = mdocOut.value
val dest2 = (laikaSite / target).value
val cmd1 =
s"npx tailwindcss -i ${src.toString}/creative-scala.css -o ${dest1.toString}/creative-scala.css"
s"cmd /c npx tailwindcss -i ${src.toString}/creative-scala.css -o ${dest1.toString}/creative-scala.css"
val cmd2 =
s"npx tailwindcss -i ${src.toString}/creative-scala.css -o ${dest2.toString}/creative-scala.css"
s"cmd /c npx tailwindcss -i ${src.toString}/creative-scala.css -o ${dest2.toString}/creative-scala.css"
cmd1 !

cmd2 !
Expand Down
13 changes: 11 additions & 2 deletions canvas/src/main/scala/doodle/canvas/algebra/CanvasAlgebra.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import cats.Apply
import cats.Eval
import cats.Functor
import cats.Monad
import doodle.algebra.generic.*
import doodle.algebra.generic._
import doodle.core.BoundingBox
import org.scalajs.dom.CanvasRenderingContext2D

Expand All @@ -29,16 +29,26 @@ final case class CanvasAlgebra(
applyDrawing: Apply[CanvasDrawing] = Apply.apply[CanvasDrawing],
functorDrawing: Functor[CanvasDrawing] = Apply.apply[CanvasDrawing]
) extends Path,
Raster,
Shape,
Immediate,
GenericDebug[CanvasDrawing],
GenericLayout[CanvasDrawing],
GenericRaster[CanvasDrawing, Immediate],
GenericShape[CanvasDrawing],
GenericSize[CanvasDrawing],
GenericStyle[CanvasDrawing],
GenericTransform[CanvasDrawing],
GivenApply[CanvasDrawing],
GivenFunctor[CanvasDrawing],
doodle.algebra.Algebra {
type Drawing[A] = doodle.canvas.Drawing[A]

override def empty: Finalized[CanvasDrawing, Unit] =
Finalized.leaf(_ =>
(BoundingBox.empty, Renderable.unit(CanvasDrawing.unit))
)

implicit val drawingInstance: Monad[Drawing] =
new Monad[Drawing] {
def pure[A](x: A): Drawing[A] =
Expand Down Expand Up @@ -67,5 +77,4 @@ final case class CanvasAlgebra(
)
}
}

}
36 changes: 19 additions & 17 deletions canvas/src/main/scala/doodle/canvas/algebra/CanvasDrawing.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
/*
* 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.canvas.algebra

import doodle.algebra.Algebra
import doodle.algebra.generic.*

import cats.Apply
import doodle.algebra.generic.Fill
import doodle.algebra.generic.Fill.ColorFill
Expand All @@ -29,21 +16,26 @@ import doodle.core.OpenPath
import doodle.core.PathElement.BezierCurveTo
import doodle.core.PathElement.LineTo
import doodle.core.PathElement.MoveTo
import doodle.core.Transform
import doodle.core.Transform
import doodle.core.font.Font
import doodle.core.font.FontFamily
import doodle.core.font.FontSize
import doodle.core.font.FontStyle
import doodle.core.font.FontWeight
import org.scalajs.dom.CanvasRenderingContext2D

import doodle.algebra.Algebra
import doodle.algebra.Raster
import doodle.algebra.Picture

import scala.scalajs.js
import scala.scalajs.js.JSConverters.*

/** A canvas `Drawing` is a function that, when applied, produces a value of
* type `A` and has the side-effect of drawing on the canvas.
*/
opaque type CanvasDrawing[A] = Function[CanvasRenderingContext2D, A]

object CanvasDrawing {
given Apply[CanvasDrawing] with {
def ap[A, B](ff: CanvasDrawing[A => B])(
Expand Down Expand Up @@ -121,6 +113,16 @@ object CanvasDrawing {
}
}

def raster(width: Int, height: Int)(
f: Immediate => Unit
): CanvasDrawing[Unit] = {
CanvasDrawing { ctx =>
implicit val implicitCtx: CanvasRenderingContext2D = ctx
val immediate = new Immediate {}
f(immediate)
}
}

def setFill(fill: Option[Fill]): CanvasDrawing[Unit] =
fill.map(setFill).getOrElse(unit)

Expand Down
36 changes: 36 additions & 0 deletions canvas/src/main/scala/doodle/canvas/algebra/Immediate.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.canvas.algebra

import doodle.core.Color
import org.scalajs.dom.CanvasRenderingContext2D

trait Immediate {
VOSID8 marked this conversation as resolved.
Show resolved Hide resolved
VOSID8 marked this conversation as resolved.
Show resolved Hide resolved
def fillColor(color: Color)(implicit ctx: CanvasRenderingContext2D): Unit = {
ctx.fillStyle = CanvasDrawing.colorToCSS(color)
}

def rectangle(x: Double, y: Double, width: Double, height: Double)(implicit ctx: CanvasRenderingContext2D): Unit = {
ctx.fillRect(x, y, width, height)
VOSID8 marked this conversation as resolved.
Show resolved Hide resolved
}
}






42 changes: 42 additions & 0 deletions canvas/src/main/scala/doodle/canvas/algebra/Raster.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.canvas.algebra

import doodle.algebra.Algebra
import doodle.algebra.generic.*
import org.scalajs.dom.CanvasRenderingContext2D
import doodle.core.Transform as Tx

trait Raster extends GenericRaster[CanvasDrawing, Immediate] {
self: Algebra { type Drawing[U] = Finalized[CanvasDrawing, U] } =>

object RasterApi extends RasterApi {
def raster(
tx: Tx,
width: Int,
height: Int
)(f: Immediate => Unit): CanvasDrawing[Unit] = {
CanvasDrawing.setTransform(tx) >>
CanvasDrawing.raster(width, height)(f)
}

def unit: CanvasDrawing[Unit] = {
CanvasDrawing.unit
}
}
}

2 changes: 2 additions & 0 deletions core/js/src/main/scala/doodle/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package object syntax {
with LayoutSyntax
with NormalizedSyntax
with PathSyntax
with RasterSyntax
with RendererSyntax
with ShapeSyntax
with SizeSyntax
Expand All @@ -41,6 +42,7 @@ package object syntax {
object layout extends LayoutSyntax
object normalized extends NormalizedSyntax
object path extends PathSyntax
object raster extends RasterSyntax
object renderer extends RendererSyntax
object shape extends ShapeSyntax
object size extends SizeSyntax
Expand Down
2 changes: 2 additions & 0 deletions core/jvm/src/main/scala/doodle/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package object syntax {
with LayoutSyntax
with NormalizedSyntax
with PathSyntax
with RasterSyntax
with RendererSyntax
with ShapeSyntax
with SizeSyntax
Expand All @@ -46,6 +47,7 @@ package object syntax {
object layout extends LayoutSyntax
object normalized extends NormalizedSyntax
object path extends PathSyntax
object raster extends RasterSyntax
object renderer extends RendererSyntax
object shape extends ShapeSyntax
object size extends SizeSyntax
Expand Down
34 changes: 34 additions & 0 deletions core/shared/src/main/scala/doodle/algebra/Raster.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 algebra

trait Raster[A] extends Algebra {
def raster(width: Int, height: Int)(f: A => Unit): Drawing[Unit]
}

trait RasterConstructor[A] {
self: BaseConstructor { type Algebra <: Raster[A] } =>

def raster(width: Int, height: Int)(f: A => Unit): Picture[Unit] =
new Picture[Unit] {
def apply(implicit algebra: Algebra): algebra.Drawing[Unit] =
algebra.raster(width, height)(f)
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 algebra
package generic

import cats.data.State
import doodle.core.BoundingBox
import doodle.core.{Transform as Tx}

trait GenericRaster[G[_], A] extends Raster[A] {
self: Algebra { type Drawing[U] = Finalized[G, U] } =>

trait RasterApi {
def raster(
tx: Tx,
width: Int,
height: Int
)(f: A => Unit): G[Unit]
def unit: G[Unit]
}

def RasterApi: RasterApi

def raster(width: Int, height: Int)(f: A => Unit): Finalized[G, Unit] = {
Finalized.leaf { dc =>
val bb = BoundingBox.centered(width, height)
(
bb,
State.inspect(tx =>
RasterApi.raster(tx, width, height)(f)
)
)
}
}

def empty: Finalized[G, Unit] =
Finalized.leaf { _ =>
(BoundingBox.empty, Renderable.unit(RasterApi.unit))
}
}
35 changes: 35 additions & 0 deletions core/shared/src/main/scala/doodle/syntax/RasterSyntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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
VOSID8 marked this conversation as resolved.
Show resolved Hide resolved
package syntax

import doodle.algebra.Algebra
import doodle.algebra.Picture
import doodle.algebra.Raster

trait RasterSyntax {
def raster[Alg <: Algebra, R](
width: Int,
height: Int
)(
f: R => Unit
): Picture[Alg & Raster[R], Unit] =
new Picture[Alg & Raster[R], Unit] {
def apply(implicit algebra: Alg & Raster[R]): algebra.Drawing[Unit] =
algebra.raster(width, height)(f)
}
}
4 changes: 4 additions & 0 deletions docs/src/pages/canvas/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ The source for these examples is [in the repository](https://github.com/creative

@:doodle("parametric-spiral", "CanvasParametricSpiral.draw")

## Experiment

@:doodle("Experiment", "Experiment.draw")


## Frame Background

Expand Down
Loading
Loading