Skip to content

Commit

Permalink
add Dom.Canvas for extra runtime safety
Browse files Browse the repository at this point in the history
  • Loading branch information
farism committed Aug 31, 2023
1 parent d3eadc5 commit 09a1850
Show file tree
Hide file tree
Showing 70 changed files with 300 additions and 226 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A simple example looks like this
```mint
component Main {
fun componentDidMount {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
el
Expand Down Expand Up @@ -42,7 +42,7 @@ Or directly view the example source files.

## Chaining

Most `Canvas` APIs return the passed in canvas `Dom.Element`. This allows you to chain calls with `|>`.
Most `Canvas` APIs return the passed in canvas `Dom.Canvas`. This allows you to chain calls with `|>`.

```mint
canvasEl
Expand Down Expand Up @@ -71,7 +71,7 @@ canvasEl

## Context2d

All APIs take `Dom.Element` as the first paramter. `getContext('2d')` is called under the hood.
All APIs take `Dom.Canvas` as the first paramter. `getContext('2d')` is called under the hood.

```mint
/*
Expand All @@ -80,12 +80,12 @@ Adds a rectangle to the current path.
Like other methods that modify the current path, this method does not directly render anything. To draw the rectangle onto a canvas, you can use the `fill()` or `stroke()` methods.
*/
fun rect (
el : Dom.Element,
el : Dom.Canvas,
x : Number,
y : Number,
width : number,
height : number
) : Dom.Element {
) : Dom.Canvas {
`#{el}.getContext('2d').rect(#{x}, #{y}, #{width}, #{height})`
el
}
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/Arc.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component Arc {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
el
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/ArcTo.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component ArcTo {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
// Tangential lines
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/BeginPath.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component BeginPath {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
// Tangential lines
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/BezierCurveTo.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component BezierCurveTo {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
let start =
Expand Down
6 changes: 4 additions & 2 deletions example/source/examples/ClearRect.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ component ClearRect {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
let dims =
Dom.getDimensions(el)
el
|> Dom.Canvas.toDomElement
|> Dom.getDimensions

// Draw yellow background
el
Expand Down
6 changes: 4 additions & 2 deletions example/source/examples/Clip.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ component Clip {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
let dims =
Dom.getDimensions(el)
el
|> Dom.Canvas.toDomElement
|> Dom.getDimensions

// Create circular clipping region
el
Expand Down
6 changes: 4 additions & 2 deletions example/source/examples/ClipPath.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ component ClipPath {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
let dims =
Dom.getDimensions(el)
el
|> Dom.Canvas.toDomElement
|> Dom.getDimensions

// Create clipping path
let region =
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/ClosePath.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component ClosePath {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
el
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/CreateConicGradient.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component CreateConicGradient {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
// Add five color stops
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/CreateImageData.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component CreateImageData {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
let imageData =
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/CreateLinearGradient.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component CreateLinearGradient {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
// Add five color stops
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/CreatePattern.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component CreatePattern {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(canvasEl) =>
case image {
Maybe::Just(imageEl) =>
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/CreateRadialGradient.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component CreateRadialGradient {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
// Add five color stops
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/DrawFocusIfNeeded.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component DrawFocusIfNeeded {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
""
Expand Down
9 changes: 6 additions & 3 deletions example/source/examples/DrawImage.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component DrawImage {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
case image {
Maybe::Just(img) =>
Expand All @@ -13,10 +13,13 @@ component DrawImage {
Dom.getDimensions(img)

// resize canvas to display full image
Dom.setAttribute(el, "width", "#{dims.width}")
Dom.setAttribute(el, "height", "#{dims.width}")
Dom.setStyle(img, "display", "none")

el
|> Dom.Canvas.toDomElement
|> Dom.setAttribute("width", "#{dims.width}")
|> Dom.setAttribute("height", "#{dims.width}")

let imageData =
CanvasImageSource.fromImage(img)

Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/Ellipse.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component Ellipse {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
el
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/Fill.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component Fill {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
el
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/FillPath.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component FillPath {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
// Create path
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/FillRect.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component FillRect {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
el
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/FillText.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component FillText {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
el
Expand Down
8 changes: 5 additions & 3 deletions example/source/examples/IsPointInPath.mint
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ component IsPointInPath {

let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
el
Expand All @@ -26,11 +26,13 @@ component IsPointInPath {
// Listen for mouse moves
let onMouseMove =
(e : Html.Event) {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
let dims =
Dom.getDimensions(el)
el
|> Dom.Canvas.toDomElement
|> Dom.getDimensions

let x =
e.clientX - dims.left
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/IsPointInStroke.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component IsPointInStroke {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
let isInStroke =
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/LineTo.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component LineTo {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
el
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/MeasureText.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component MeasureText {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
let width =
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/MoveTo.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component MoveTo {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
el
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/QuadraticCurveTo.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component QuadraticCurveTo {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
// Quadratic Bézier curve
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/Rect.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component Rect {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
el
Expand Down
4 changes: 2 additions & 2 deletions example/source/examples/Reset.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component Reset {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
el
Expand All @@ -21,7 +21,7 @@ component Reset {

let reset =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
el
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/ResetTransform.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component ResetTransform {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
// Skewed rectangles
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/Restore.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component Restore {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
// Save the current state
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/Rotate.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component Rotate {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
// Point of transform origin
Expand Down
2 changes: 1 addition & 1 deletion example/source/examples/RoundRect.mint
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ component RoundRect {
@format {
let draw =
() {
case canvas {
case Dom.Canvas.fromDomElement(canvas) {
Maybe::Just(el) =>
{
// Rounded rectangle with zero radius (specified as a number)
Expand Down
Loading

0 comments on commit 09a1850

Please sign in to comment.