-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First working functionality for Canvas backend
- Loading branch information
Showing
17 changed files
with
518 additions
and
9 deletions.
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
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,19 @@ | ||
/* | ||
* 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 | ||
|
||
type Algebra = doodle.canvas.algebra.Algebra |
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,20 @@ | ||
/* | ||
* 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 | ||
|
||
type Drawing[A] = | ||
doodle.algebra.generic.Finalized[doodle.canvas.algebra.CanvasDrawing, A] |
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,20 @@ | ||
/* | ||
* 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 | ||
|
||
type Frame = doodle.canvas.effect.Frame | ||
val Frame = doodle.canvas.effect.Frame |
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,27 @@ | ||
/* | ||
* 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 | ||
|
||
import doodle.algebra.BaseConstructor | ||
import doodle.algebra.ShapeConstructor | ||
|
||
type Picture[A] = doodle.algebra.Picture[Algebra, A] | ||
object Picture extends BaseConstructor, ShapeConstructor { | ||
|
||
type Algebra = doodle.canvas.Algebra | ||
type Drawing[A] = doodle.canvas.Drawing[A] | ||
} |
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,70 @@ | ||
/* | ||
* 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 cats.Apply | ||
import cats.Eval | ||
import cats.Functor | ||
import cats.Monad | ||
import doodle.algebra.generic._ | ||
import doodle.core.BoundingBox | ||
import org.scalajs.dom.CanvasRenderingContext2D | ||
|
||
final case class Algebra( | ||
ctx: CanvasRenderingContext2D, | ||
applyDrawing: Apply[CanvasDrawing] = Apply.apply[CanvasDrawing], | ||
functorDrawing: Functor[CanvasDrawing] = Apply.apply[CanvasDrawing] | ||
) extends Shape, | ||
GenericDebug[CanvasDrawing], | ||
GenericLayout[CanvasDrawing], | ||
GenericSize[CanvasDrawing], | ||
GenericStyle[CanvasDrawing], | ||
GenericTransform[CanvasDrawing], | ||
GivenApply[CanvasDrawing], | ||
GivenFunctor[CanvasDrawing], | ||
doodle.algebra.Algebra { | ||
type Drawing[A] = doodle.canvas.Drawing[A] | ||
implicit val drawingInstance: Monad[Drawing] = | ||
new Monad[Drawing] { | ||
def pure[A](x: A): Drawing[A] = | ||
Finalized.leaf(_ => | ||
( | ||
BoundingBox.empty, | ||
Renderable.apply(_ => Eval.now(CanvasDrawing.pure(x))) | ||
) | ||
) | ||
|
||
def flatMap[A, B](fa: Drawing[A])(f: A => Drawing[B]): Drawing[B] = | ||
fa.flatMap { (bb, rdr) => | ||
val canvasDrawing = rdr.runA(doodle.core.Transform.identity).value | ||
val a = canvasDrawing(ctx) | ||
f(a) | ||
} | ||
|
||
def tailRecM[A, B](a: A)(f: A => Drawing[Either[A, B]]): Drawing[B] = { | ||
// TODO: This implementation is not tail recursive but I don't think we need it for what we use in Doodle | ||
val dAB = f(a) | ||
flatMap(dAB)(either => | ||
either match { | ||
case Left(a) => tailRecM(a)(f) | ||
case Right(b) => dAB.asInstanceOf[Drawing[B]] | ||
} | ||
) | ||
} | ||
} | ||
|
||
} |
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
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
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,56 @@ | ||
/* | ||
* 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.effect | ||
|
||
import cats.effect.IO | ||
import doodle.algebra.generic.Finalized | ||
import doodle.canvas.Picture | ||
import doodle.canvas.algebra.Algebra | ||
import doodle.canvas.algebra.CanvasDrawing | ||
import doodle.core.Transform | ||
import org.scalajs.dom | ||
|
||
final case class Canvas(target: dom.Node, frame: Frame) { | ||
val canvas: dom.html.Canvas = | ||
dom.document.createElement("canvas").asInstanceOf[dom.html.Canvas] | ||
val ctx = canvas.getContext("2d").asInstanceOf[dom.CanvasRenderingContext2D] | ||
val algebra = Algebra(ctx) | ||
|
||
target.appendChild(canvas) | ||
|
||
def render[A](picture: Picture[A]): IO[A] = { | ||
IO { | ||
val finalized: Finalized[CanvasDrawing, A] = picture(algebra) | ||
val (bb, rdr) = finalized.run(List.empty).value | ||
val drawing = rdr.runA(Transform.identity).value | ||
val a = drawing(ctx) | ||
a | ||
} | ||
} | ||
} | ||
object Canvas { | ||
def fromFrame(frame: Frame): IO[Canvas] = { | ||
IO { | ||
val target = dom.document.getElementById(frame.id) | ||
if (target == null) { | ||
throw new java.util.NoSuchElementException( | ||
s"Doodle Canvas could not be created, as could not find a DOM element with the requested id ${frame.id}" | ||
) | ||
} else Canvas(target, frame) | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
canvas/src/main/scala/doodle/canvas/effect/CanvasRenderer.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,31 @@ | ||
/* | ||
* 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.effect | ||
|
||
import cats.effect.IO | ||
import doodle.canvas.Algebra | ||
import doodle.canvas.Picture | ||
import doodle.effect.Renderer | ||
|
||
object CanvasRenderer extends Renderer[Algebra, Frame, Canvas] { | ||
|
||
def canvas(description: Frame): IO[Canvas] = | ||
Canvas.fromFrame(description) | ||
|
||
def render[A](canvas: Canvas)(picture: Picture[A]): IO[A] = | ||
canvas.render(picture) | ||
} |
Oops, something went wrong.