From d24b7265a3f6a442ca6c45c35fa8e88b825de2f8 Mon Sep 17 00:00:00 2001 From: Guillaume Bagan Date: Wed, 10 May 2023 22:13:44 +0200 Subject: [PATCH 1/7] Update FFI to EffectFnX types --- bower.json | 29 -- package.json | 6 +- packages.dhall | 105 +++++++ spago.dhall | 6 + src/Graphics/Canvas.js | 650 +++++++++++---------------------------- src/Graphics/Canvas.purs | 316 ++++++++++++++----- 6 files changed, 529 insertions(+), 583 deletions(-) delete mode 100644 bower.json create mode 100644 packages.dhall create mode 100644 spago.dhall diff --git a/bower.json b/bower.json deleted file mode 100644 index bb8505d..0000000 --- a/bower.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "name": "purescript-canvas", - "authors": [ - "Phil Freeman " - ], - "description": "Canvas bindings", - "keywords": [ - "purescript" - ], - "license": "MIT", - "ignore": [ - "**/.*", - "node_modules", - "bower_components", - "test", - "tests" - ], - "repository": { - "type": "git", - "url": "https://github.com/purescript-web/purescript-canvas.git" - }, - "dependencies": { - "purescript-arraybuffer-types": "^3.0.2", - "purescript-effect": "^4.0.0", - "purescript-exceptions": "^6.0.0", - "purescript-functions": "^6.0.0", - "purescript-maybe": "^6.0.0" - } -} diff --git a/package.json b/package.json index d2c3151..bd93a90 100644 --- a/package.json +++ b/package.json @@ -2,12 +2,12 @@ "private": true, "scripts": { "clean": "rimraf output && rimraf .pulp-cache", - "build": "eslint src && pulp build -- --censor-lib --strict" + "build": "eslint src && spago build" }, "devDependencies": { "eslint": "^7.32.0", - "pulp": "16.0.0-0", "purescript-psa": "^0.8.2", - "rimraf": "^3.0.2" + "rimraf": "^3.0.2", + "spago": "^0.21.0" } } diff --git a/packages.dhall b/packages.dhall new file mode 100644 index 0000000..a343ac4 --- /dev/null +++ b/packages.dhall @@ -0,0 +1,105 @@ +{- +Welcome to your new Dhall package-set! + +Below are instructions for how to edit this file for most use +cases, so that you don't need to know Dhall to use it. + +## Use Cases + +Most will want to do one or both of these options: +1. Override/Patch a package's dependency +2. Add a package not already in the default package set + +This file will continue to work whether you use one or both options. +Instructions for each option are explained below. + +### Overriding/Patching a package + +Purpose: +- Change a package's dependency to a newer/older release than the + default package set's release +- Use your own modified version of some dependency that may + include new API, changed API, removed API by + using your custom git repo of the library rather than + the package set's repo + +Syntax: +where `entityName` is one of the following: +- dependencies +- repo +- version +------------------------------- +let upstream = -- +in upstream + with packageName.entityName = "new value" +------------------------------- + +Example: +------------------------------- +let upstream = -- +in upstream + with halogen.version = "master" + with halogen.repo = "https://example.com/path/to/git/repo.git" + + with halogen-vdom.version = "v4.0.0" + with halogen-vdom.dependencies = [ "extra-dependency" ] # halogen-vdom.dependencies +------------------------------- + +### Additions + +Purpose: +- Add packages that aren't already included in the default package set + +Syntax: +where `` is: +- a tag (i.e. "v4.0.0") +- a branch (i.e. "master") +- commit hash (i.e. "701f3e44aafb1a6459281714858fadf2c4c2a977") +------------------------------- +let upstream = -- +in upstream + with new-package-name = + { dependencies = + [ "dependency1" + , "dependency2" + ] + , repo = + "https://example.com/path/to/git/repo.git" + , version = + "" + } +------------------------------- + +Example: +------------------------------- +let upstream = -- +in upstream + with benchotron = + { dependencies = + [ "arrays" + , "exists" + , "profunctor" + , "strings" + , "quickcheck" + , "lcg" + , "transformers" + , "foldable-traversable" + , "exceptions" + , "node-fs" + , "node-buffer" + , "node-readline" + , "datetime" + , "now" + ] + , repo = + "https://github.com/hdgarrood/purescript-benchotron.git" + , version = + "v7.0.0" + } +------------------------------- +-} +let upstream = + https://github.com/purescript/package-sets/releases/download/psc-0.15.8-20230510/packages.dhall + sha256:a547aee77b205322514014f5d446f9b493d1731944790cb286aabd6d7d97cfda + +in upstream diff --git a/spago.dhall b/spago.dhall new file mode 100644 index 0000000..853c48e --- /dev/null +++ b/spago.dhall @@ -0,0 +1,6 @@ +{ name = "canvas" +, dependencies = + [ "arraybuffer-types", "console", "effect", "exceptions", "maybe", "prelude" ] +, packages = ./packages.dhall +, sources = [ "src/**/*.purs" ] +} diff --git a/src/Graphics/Canvas.js b/src/Graphics/Canvas.js index 793186b..2b9f1a5 100644 --- a/src/Graphics/Canvas.js +++ b/src/Graphics/Canvas.js @@ -2,392 +2,212 @@ export function canvasElementToImageSource(e) { return e; } -export function tryLoadImageImpl(src) { - return function(e) { - return function(f) { - return function () { - var img = new Image(); - img.src = src; - img.addEventListener("load", function() { - f(img)(); - }, false); - img.addEventListener("error", function() { - e(); - }, false); - }; - }; - }; +export function tryLoadImageImpl(src, e, f) { + var img = new Image(); + img.src = src; + img.addEventListener("load", function() { + f(img)(); + }, false); + img.addEventListener("error", function() { + e(); + }, false); } export function getCanvasElementByIdImpl(id, Just, Nothing) { - return function() { - var el = document.getElementById(id); - if (el && el instanceof HTMLCanvasElement) { - return Just(el); - } else { - return Nothing; - } - }; + var el = document.getElementById(id); + if (el && el instanceof HTMLCanvasElement) { + return Just(el); + } else { + return Nothing; + } } -export function getContext2D(c) { - return function() { - return c.getContext("2d"); - }; +export function getContext2DImpl(c) { + return c.getContext("2d"); } -export function getCanvasWidth(canvas) { - return function() { - return canvas.width; - }; +export function getCanvasWidthImpl(canvas) { + return canvas.width; } -export function getCanvasHeight(canvas) { - return function() { - return canvas.height; - }; +export function getCanvasHeightImpl(canvas) { + return canvas.height; } -export function setCanvasWidth(canvas) { - return function(width) { - return function() { - canvas.width = width; - }; - }; +export function setCanvasWidthImpl(canvas, width) { + canvas.width = width; } -export function setCanvasHeight(canvas) { - return function(height) { - return function() { - canvas.height = height; - }; - }; +export function setCanvasHeightImpl(canvas, height) { + canvas.height = height; } -export function canvasToDataURL(canvas) { - return function() { - return canvas.toDataURL(); - }; +export function canvasToDataURLImpl(canvas) { + return canvas.toDataURL(); } -export function setLineWidth(ctx) { - return function(width) { - return function() { - ctx.lineWidth = width; - }; - }; +export function setLineWidthImpl(ctx, width) { + ctx.lineWidth = width; } -export function setLineDash(ctx) { - return function(dash) { - return function() { - ctx.setLineDash(dash); - }; - }; +export function setLineDashImpl(ctx, dash) { + ctx.setLineDash(dash); } -export function setFillStyle(ctx) { - return function(style) { - return function() { - ctx.fillStyle = style; - }; - }; +export function setFillStyleImpl(ctx, style) { + ctx.fillStyle = style; } -export function setStrokeStyle(ctx) { - return function(style) { - return function() { - ctx.strokeStyle = style; - }; - }; +export function setStrokeStyleImpl(ctx, style) { + ctx.strokeStyle = style; } -export function setShadowColor(ctx) { - return function(color) { - return function() { - ctx.shadowColor = color; - }; - }; +export function setShadowColorImpl(ctx, color) { + ctx.shadowColor = color; } -export function setShadowBlur(ctx) { - return function(blur) { - return function() { - ctx.shadowBlur = blur; - }; - }; +export function setShadowBlurImpl(ctx, blur) { + ctx.shadowBlur = blur; } -export function setShadowOffsetX(ctx) { - return function(offsetX) { - return function() { - ctx.shadowOffsetX = offsetX; - }; - }; +export function setShadowOffsetXImpl(ctx, offsetX) { + ctx.shadowOffsetX = offsetX; } -export function setShadowOffsetY(ctx) { - return function(offsetY) { - return function() { - ctx.shadowOffsetY = offsetY; - }; - }; +export function setShadowOffsetYImpl(ctx, offsetY) { + ctx.shadowOffsetY = offsetY; } -export function setMiterLimit(ctx) { - return function(limit) { - return function() { - ctx.miterLimit = limit; - }; - }; +export function setMiterLimitImpl(ctx, limit) { + ctx.miterLimit = limit; } -export function setLineCapImpl(ctx) { - return function(cap) { - return function() { - ctx.lineCap = cap; - }; - }; +export function setLineCapImpl(ctx, cap) { + ctx.lineCap = cap; } -export function setLineJoinImpl(ctx) { - return function(join) { - return function() { - ctx.lineJoin = join; - }; - }; +export function setLineJoinImpl(ctx, join) { + ctx.lineJoin = join; } -export function setGlobalCompositeOperationImpl(ctx) { - return function(op) { - return function() { - ctx.globalCompositeOperation = op; - }; - }; +export function setGlobalCompositeOperationImpl(ctx, op) { + ctx.globalCompositeOperation = op; } -export function setGlobalAlpha(ctx) { - return function(alpha) { - return function() { - ctx.globalAlpha = alpha; - }; - }; +export function setGlobalAlphaImpl(ctx, alpha) { + ctx.globalAlpha = alpha; } -export function beginPath(ctx) { - return function() { - ctx.beginPath(); - }; +export function beginPathImpl(ctx) { + ctx.beginPath(); } -export function stroke(ctx) { - return function() { - ctx.stroke(); - }; +export function strokeImpl(ctx) { + ctx.stroke(); } -export function fill(ctx) { - return function() { - ctx.fill(); - }; +export function fillImpl(ctx) { + ctx.fill(); } -export function clip(ctx) { - return function() { - ctx.clip(); - }; +export function clipImpl(ctx) { + ctx.clip(); } -export function lineTo(ctx) { - return function(x) { - return function(y) { - return function() { - ctx.lineTo(x, y); - }; - }; - }; +export function lineToImpl(ctx, x, y) { + ctx.lineTo(x, y); } -export function moveTo(ctx) { - return function(x) { - return function(y) { - return function() { - ctx.moveTo(x, y); - }; - }; - }; +export function moveToImpl(ctx, x, y) { + ctx.moveTo(x, y); } -export function closePath(ctx) { - return function() { - ctx.closePath(); - }; +export function closePathImpl(ctx) { + ctx.closePath(); } -export function arc(ctx) { - return function(a) { - return function() { - ctx.arc(a.x, a.y, a.radius, a.start, a.end, a.useCounterClockwise); - }; - }; +export function arcImpl(ctx, a) { + ctx.arc(a.x, a.y, a.radius, a.start, a.end, a.useCounterClockwise); } -export function rect(ctx) { - return function(r) { - return function() { - ctx.rect(r.x, r.y, r.width, r.height); - }; - }; +export function rectImpl(ctx, r) { + ctx.rect(r.x, r.y, r.width, r.height); } -export function fillRect(ctx) { - return function(r) { - return function() { - ctx.fillRect(r.x, r.y, r.width, r.height); - }; - }; +export function fillRectImpl(ctx, r) { + ctx.fillRect(r.x, r.y, r.width, r.height); } -export function strokeRect(ctx) { - return function(r) { - return function() { - ctx.strokeRect(r.x, r.y, r.width, r.height); - }; - }; +export function strokeRectImpl(ctx, r) { + ctx.strokeRect(r.x, r.y, r.width, r.height); } -export function clearRect(ctx) { - return function(r) { - return function() { - ctx.clearRect(r.x, r.y, r.width, r.height); - }; - }; +export function clearRectImpl(ctx, r) { + ctx.clearRect(r.x, r.y, r.width, r.height); } -export function scale(ctx) { - return function(t) { - return function() { - ctx.scale(t.scaleX, t.scaleY); - }; - }; +export function scaleImpl(ctx, t) { + ctx.scale(t.scaleX, t.scaleY); } -export function rotate(ctx) { - return function(angle) { - return function() { - ctx.rotate(angle); - }; - }; +export function rotateImpl(ctx, angle) { + ctx.rotate(angle); } -export function translate(ctx) { - return function(t) { - return function() { - ctx.translate(t.translateX, t.translateY); - }; - }; +export function translateImpl(ctx, t) { + ctx.translate(t.translateX, t.translateY); } -export function transform(ctx) { - return function(t) { - return function() { - ctx.transform(t.a, t.b, t.c, t.d, t.e, t.f); - }; - }; +export function transformImpl(ctx, t) { + ctx.transform(t.a, t.b, t.c, t.d, t.e, t.f); } -export function setTransform(ctx) { - return function(t) { - return function() { - ctx.setTransform(t.a, t.b, t.c, t.d, t.e, t.f); - }; - }; +export function setTransformImpl(ctx, t) { + ctx.setTransform(t.a, t.b, t.c, t.d, t.e, t.f); } export function textAlignImpl(ctx) { - return function() { - return ctx.textAlign; - }; + return ctx.textAlign; } -export function setTextAlignImpl(ctx) { - return function(textAlign) { - return function() { - ctx.textAlign = textAlign; - }; - }; +export function setTextAlignImpl(ctx, textAlign) { + ctx.textAlign = textAlign; } export function textBaselineImpl(ctx) { - return function () { - return ctx.textBaseline; - }; + return ctx.textBaseline; } -export function setTextBaselineImpl(ctx) { - return function (textBaseline) { - return function () { - ctx.textBaseline = textBaseline; - }; - }; +export function setTextBaselineImpl(ctx, textBaseline) { + ctx.textBaseline = textBaseline; } -export function font(ctx) { - return function() { - return ctx.font; - }; +export function fontImpl(ctx) { + return ctx.font; } -export function setFont(ctx) { - return function(fontspec) { - return function() { - ctx.font = fontspec; - }; - }; +export function setFontImpl(ctx, fontspec) { + ctx.font = fontspec; } -export function fillText(ctx) { - return function(text) { - return function(x) { - return function(y) { - return function() { - ctx.fillText(text, x, y); - }; - }; - }; - }; +export function fillTextImpl(ctx, text, x, y) { + ctx.fillText(text, x, y); } -export function strokeText(ctx) { - return function(text) { - return function(x) { - return function(y) { - return function() { - ctx.strokeText(text, x, y); - }; - }; - }; - }; +export function strokeTextImpl(ctx, text, x, y) { + ctx.strokeText(text, x, y); } -export function measureText(ctx) { - return function(text) { - return function() { - return ctx.measureText(text); - }; - }; +export function measureTextImpl(ctx, text) { + return ctx.measureText(text); } -export function save(ctx) { - return function() { - ctx.save(); - }; +export function saveImpl(ctx) { + ctx.save(); } -export function restore(ctx) { - return function() { - ctx.restore(); - }; +export function restoreImpl(ctx) { + ctx.restore(); } export function imageDataWidth(image) { @@ -402,194 +222,70 @@ export function imageDataBuffer(image) { return image.data; } -export function getImageData(ctx) { - return function(x) { - return function(y) { - return function(w) { - return function(h) { - return function() { - return ctx.getImageData(x, y, w, h); - }; - }; - }; - }; - }; -} - -export function putImageDataFull(ctx) { - return function(image_data) { - return function(x) { - return function(y) { - return function(dx) { - return function(dy) { - return function(dw) { - return function(dh) { - return function() { - ctx.putImageData(image_data, x, y, dx, dy, dw, dh); - }; - }; - }; - }; - }; - }; - }; - }; -} - -export function putImageData(ctx) { - return function(image_data) { - return function(x) { - return function(y) { - return function() { - ctx.putImageData(image_data, x, y); - }; - }; - }; - }; -} - -export function createImageData(ctx) { - return function(sw) { - return function(sh) { - return function() { - return ctx.createImageData(sw, sh); - }; - }; - }; -} - -export function createImageDataCopy(ctx) { - return function(image_data) { - return function() { - return ctx.createImageData(image_data); - }; - }; -} - -export function createImageDataWith(arr) { - return function(sw) { - return function() { - return new ImageData(arr, sw); - }; - }; -} - -export function drawImage(ctx) { - return function(image_source) { - return function(dx) { - return function(dy) { - return function() { - ctx.drawImage(image_source, dx, dy); - }; - }; - }; - }; -} - -export function drawImageScale(ctx) { - return function(image_source) { - return function(dx) { - return function(dy) { - return function(dWidth) { - return function(dHeight) { - return function() { - ctx.drawImage(image_source, dx, dy, dWidth, dHeight); - }; - }; - }; - }; - }; - }; -} - -export function drawImageFull(ctx) { - return function(image_source) { - return function(sx) { - return function(sy) { - return function(sWidth) { - return function(sHeight) { - return function(dx) { - return function(dy) { - return function(dWidth) { - return function(dHeight) { - return function() { - ctx.drawImage(image_source, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight); - }; - }; - }; - }; - }; - }; - }; - }; - }; - }; -} - -export function createPatternImpl(ctx) { - return function(img) { - return function(repeat) { - return function() { - return ctx.createPattern(img, repeat); - }; - }; - }; -} - -export function setPatternFillStyle(ctx) { - return function(pattern) { - return function() { - ctx.fillStyle = pattern; - }; - }; -} - -export function createLinearGradient(ctx) { - return function(linearGradient) { - return function() { - return ctx.createLinearGradient(linearGradient.x0, linearGradient.y0, linearGradient.x1, linearGradient.y1); - }; - }; -} - -export function createRadialGradient(ctx) { - return function(radialGradient) { - return function() { - return ctx.createRadialGradient(radialGradient.x0, radialGradient.y0, radialGradient.r0, radialGradient.x1, radialGradient.y1, radialGradient.r1); - }; - }; -} - -export function addColorStop(gradient) { - return function(stop) { - return function(color) { - return function() { - gradient.addColorStop(stop, color); - }; - }; - }; -} - -export function setGradientFillStyle(ctx) { - return function(gradient) { - return function() { - ctx.fillStyle = gradient; - }; - }; -} - -export function quadraticCurveTo(ctx) { - return function(qCurve) { - return function() { - ctx.quadraticCurveTo(qCurve.cpx, qCurve.cpy, qCurve.x, qCurve.y); - }; - }; -} - -export function bezierCurveTo(ctx) { - return function(bCurve) { - return function() { - ctx.bezierCurveTo(bCurve.cp1x, bCurve.cp1y, bCurve.cp2x, bCurve.cp2y, bCurve.x, bCurve.y); - }; - }; +export function getImageDataImpl(ctx, x, y, w, h) { + return ctx.getImageData(x, y, w, h); +} + +export function putImageDataFullImpl(ctx, image_data, x, y, dx, dy, dw, dh) { + ctx.putImageData(image_data, x, y, dx, dy, dw, dh); +} + +export function putImageDataImpl(ctx, image_data, x, y) { + ctx.putImageData(image_data, x, y); +} + +export function createImageDataImpl(ctx, sw, sh) { + return ctx.createImageData(sw, sh); +} + +export function createImageDataCopyImpl(ctx, image_data) { + return ctx.createImageData(image_data); +} + +export function createImageDataWithImpl(arr, sw) { + return new ImageData(arr, sw); +} + +export function drawImageImpl(ctx, image_source, dx, dy) { + ctx.drawImage(image_source, dx, dy); +} + +export function drawImageScaleImpl(ctx, image_source, dx, dy, dWidth, dHeight) { + ctx.drawImage(image_source, dx, dy, dWidth, dHeight); +} + +export function drawImageFullImpl(ctx, image_source, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) { + ctx.drawImage(image_source, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight); +} + +export function createPatternImpl(ctx, img, repeat) { + return ctx.createPattern(img, repeat); +} + +export function setPatternFillStyleImpl(ctx, pattern) { + ctx.fillStyle = pattern; +} + +export function createLinearGradientImpl(ctx, linearGradient) { + return ctx.createLinearGradient(linearGradient.x0, linearGradient.y0, linearGradient.x1, linearGradient.y1); +} + +export function createRadialGradientImpl(ctx, radialGradient) { + return ctx.createRadialGradient(radialGradient.x0, radialGradient.y0, radialGradient.r0, radialGradient.x1, radialGradient.y1, radialGradient.r1); +} + +export function addColorStopImpl(gradient, stop, color) { + gradient.addColorStop(stop, color); +} + +export function setGradientFillStyleImpl(ctx, gradient) { + ctx.fillStyle = gradient; +} + +export function quadraticCurveToImpl(ctx, qCurve) { + ctx.quadraticCurveTo(qCurve.cpx, qCurve.cpy, qCurve.x, qCurve.y); +} + +export function bezierCurveToImpl(ctx, bCurve) { + ctx.bezierCurveTo(bCurve.cp1x, bCurve.cp1y, bCurve.cp2x, bCurve.cp2y, bCurve.x, bCurve.y); } diff --git a/src/Graphics/Canvas.purs b/src/Graphics/Canvas.purs index 428cd03..b04a1ab 100644 --- a/src/Graphics/Canvas.purs +++ b/src/Graphics/Canvas.purs @@ -119,9 +119,10 @@ module Graphics.Canvas import Prelude import Effect (Effect) +import Effect.Uncurried ( EffectFn1, EffectFn2, EffectFn3, EffectFn4, EffectFn5, EffectFn6, EffectFn8, EffectFn10 + , runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4, runEffectFn5, runEffectFn6, runEffectFn8, runEffectFn10) import Effect.Exception.Unsafe (unsafeThrow) import Data.ArrayBuffer.Types (Uint8ClampedArray) -import Data.Function.Uncurried (Fn3, runFn3) import Data.Maybe (Maybe(..)) -- | A canvas HTML element. @@ -158,30 +159,42 @@ tryLoadImage tryLoadImage path k = tryLoadImageImpl path (k Nothing) (k <<< Just) foreign import getCanvasElementByIdImpl - :: forall r - . Fn3 String - (CanvasElement -> r) - r - (Effect r) + :: forall r. EffectFn3 String (CanvasElement -> r) r r -- | Get a canvas element by ID, or `Nothing` if the element does not exist. getCanvasElementById :: String -> Effect (Maybe CanvasElement) -getCanvasElementById elId = runFn3 getCanvasElementByIdImpl elId Just Nothing +getCanvasElementById elId = runEffectFn3 getCanvasElementByIdImpl elId Just Nothing + + +foreign import getContext2DImpl :: EffectFn1 CanvasElement Context2D -- | Get the 2D graphics context for a canvas element. -foreign import getContext2D :: CanvasElement -> Effect Context2D +getContext2D :: CanvasElement -> Effect Context2D +getContext2D el = runEffectFn1 getContext2DImpl el + +foreign import getCanvasWidthImpl :: EffectFn1 CanvasElement Number -- | Get the canvas width in pixels. -foreign import getCanvasWidth :: CanvasElement -> Effect Number +getCanvasWidth :: CanvasElement -> Effect Number +getCanvasWidth el = runEffectFn1 getCanvasWidthImpl el + +foreign import getCanvasHeightImpl :: EffectFn1 CanvasElement Number -- | Get the canvas height in pixels. -foreign import getCanvasHeight :: CanvasElement -> Effect Number +getCanvasHeight :: CanvasElement -> Effect Number +getCanvasHeight el = runEffectFn1 getCanvasHeightImpl el + +foreign import setCanvasWidthImpl :: EffectFn2 CanvasElement Number Unit -- | Set the canvas width in pixels. -foreign import setCanvasWidth :: CanvasElement -> Number -> Effect Unit +setCanvasWidth :: CanvasElement -> Number -> Effect Unit +setCanvasWidth el w = runEffectFn2 setCanvasWidthImpl el w + +foreign import setCanvasHeightImpl :: EffectFn2 CanvasElement Number Unit -- | Set the canvas height in pixels. -foreign import setCanvasHeight :: CanvasElement -> Number -> Effect Unit +setCanvasHeight :: CanvasElement -> Number -> Effect Unit +setCanvasHeight el w = runEffectFn2 setCanvasHeightImpl el w -- | Canvas dimensions (width and height) in pixels. type Dimensions = { width :: Number, height :: Number } @@ -197,35 +210,65 @@ getCanvasDimensions ce = do setCanvasDimensions :: CanvasElement -> Dimensions -> Effect Unit setCanvasDimensions ce d = setCanvasHeight ce d.height *> setCanvasWidth ce d.width +foreign import canvasToDataURLImpl :: EffectFn1 CanvasElement String + -- | Create a data URL for the current canvas contents -foreign import canvasToDataURL :: CanvasElement -> Effect String +canvasToDataURL :: CanvasElement -> Effect String +canvasToDataURL el = runEffectFn1 canvasToDataURLImpl el + +foreign import setLineWidthImpl :: EffectFn2 Context2D Number Unit -- | Set the current line width. -foreign import setLineWidth :: Context2D -> Number -> Effect Unit +setLineWidth :: Context2D -> Number -> Effect Unit +setLineWidth ctx w = runEffectFn2 setLineWidthImpl ctx w + +foreign import setLineDashImpl :: EffectFn2 Context2D (Array Number) Unit -- | Set the current line dash pattern. -foreign import setLineDash :: Context2D -> Array Number -> Effect Unit +setLineDash :: Context2D -> Array Number -> Effect Unit +setLineDash ctx arr = runEffectFn2 setLineDashImpl ctx arr + +foreign import setFillStyleImpl :: EffectFn2 Context2D String Unit -- | Set the current fill style/color. -foreign import setFillStyle :: Context2D -> String -> Effect Unit +setFillStyle :: Context2D -> String -> Effect Unit +setFillStyle ctx style = runEffectFn2 setFillStyleImpl ctx style + +foreign import setStrokeStyleImpl :: EffectFn2 Context2D String Unit -- | Set the current stroke style/color. -foreign import setStrokeStyle :: Context2D -> String -> Effect Unit +setStrokeStyle :: Context2D -> String -> Effect Unit +setStrokeStyle ctx style = runEffectFn2 setStrokeStyleImpl ctx style + +foreign import setShadowColorImpl :: EffectFn2 Context2D String Unit -- | Set the current shadow color. -foreign import setShadowColor :: Context2D -> String -> Effect Unit +setShadowColor :: Context2D -> String -> Effect Unit +setShadowColor ctx color = runEffectFn2 setShadowColorImpl ctx color + +foreign import setShadowBlurImpl :: EffectFn2 Context2D Number Unit -- | Set the current shadow blur radius. -foreign import setShadowBlur :: Context2D -> Number -> Effect Unit +setShadowBlur :: Context2D -> Number -> Effect Unit +setShadowBlur ctx radius = runEffectFn2 setShadowBlurImpl ctx radius + +foreign import setShadowOffsetXImpl :: EffectFn2 Context2D Number Unit + +-- | Set the current shadow x-offset. +setShadowOffsetX :: Context2D -> Number -> Effect Unit +setShadowOffsetX ctx x = runEffectFn2 setShadowOffsetXImpl ctx x + +foreign import setShadowOffsetYImpl :: EffectFn2 Context2D Number Unit -- | Set the current shadow x-offset. -foreign import setShadowOffsetX :: Context2D -> Number -> Effect Unit +setShadowOffsetY :: Context2D -> Number -> Effect Unit +setShadowOffsetY ctx y = runEffectFn2 setShadowOffsetYImpl ctx y --- | Set the current shadow y-offset. -foreign import setShadowOffsetY :: Context2D -> Number -> Effect Unit +foreign import setMiterLimitImpl :: EffectFn2 Context2D Number Unit -- | Set the current miter limit. -foreign import setMiterLimit :: Context2D -> Number -> Effect Unit +setMiterLimit :: Context2D -> Number -> Effect Unit +setMiterLimit ctx limit = runEffectFn2 setMiterLimitImpl ctx limit -- | Enumerates the different types of line cap. data LineCap = Round | Square | Butt @@ -317,11 +360,11 @@ instance showComposite :: Show Composite where show Color = "Color" show Luminosity = "Luminosity" -foreign import setGlobalCompositeOperationImpl :: Context2D -> String -> Effect Unit +foreign import setGlobalCompositeOperationImpl :: EffectFn2 Context2D String Unit -- | Set the current composite operation. setGlobalCompositeOperation :: Context2D -> Composite -> Effect Unit -setGlobalCompositeOperation ctx composite = setGlobalCompositeOperationImpl ctx (toString composite) +setGlobalCompositeOperation ctx composite = runEffectFn2 setGlobalCompositeOperationImpl ctx (toString composite) where toString SourceOver = "source-over" toString SourceIn = "source-in" @@ -350,29 +393,53 @@ setGlobalCompositeOperation ctx composite = setGlobalCompositeOperationImpl ctx toString Color = "color" toString Luminosity = "luminosity" +foreign import setGlobalAlphaImpl :: EffectFn2 Context2D Number Unit + -- | Set the current global alpha level. -foreign import setGlobalAlpha :: Context2D -> Number -> Effect Unit +setGlobalAlpha :: Context2D -> Number -> Effect Unit +setGlobalAlpha ctx level = runEffectFn2 setGlobalAlphaImpl ctx level + +foreign import beginPathImpl :: EffectFn1 Context2D Unit -- | Begin a path object. -foreign import beginPath :: Context2D -> Effect Unit +beginPath :: Context2D -> Effect Unit +beginPath ctx = runEffectFn1 beginPathImpl ctx + +foreign import strokeImpl :: EffectFn1 Context2D Unit -- | Stroke the current object. -foreign import stroke :: Context2D -> Effect Unit +stroke :: Context2D -> Effect Unit +stroke ctx = runEffectFn1 strokeImpl ctx + +foreign import fillImpl :: EffectFn1 Context2D Unit -- | Fill the current object. -foreign import fill :: Context2D -> Effect Unit +fill :: Context2D -> Effect Unit +fill ctx = runEffectFn1 fillImpl ctx + +foreign import clipImpl :: EffectFn1 Context2D Unit -- | Clip to the current object. -foreign import clip :: Context2D -> Effect Unit +clip :: Context2D -> Effect Unit +clip ctx = runEffectFn1 clipImpl ctx + +foreign import lineToImpl :: EffectFn3 Context2D Number Number Unit -- | Move the path to the specified coordinates, drawing a line segment. -foreign import lineTo :: Context2D -> Number -> Number -> Effect Unit +lineTo :: Context2D -> Number -> Number -> Effect Unit +lineTo ctx x y = runEffectFn3 lineToImpl ctx x y + +foreign import moveToImpl :: EffectFn3 Context2D Number Number Unit -- | Move the path to the specified coordinates, without drawing a line segment. -foreign import moveTo :: Context2D -> Number -> Number -> Effect Unit +moveTo :: Context2D -> Number -> Number -> Effect Unit +moveTo ctx x y = runEffectFn3 moveToImpl ctx x y + +foreign import closePathImpl :: EffectFn1 Context2D Unit -- | Close the current path. -foreign import closePath :: Context2D -> Effect Unit +closePath :: Context2D -> Effect Unit +closePath ctx = runEffectFn1 closePathImpl ctx -- | A convenience function for drawing a stroked path. -- | @@ -426,8 +493,11 @@ type Arc = , useCounterClockwise :: Boolean } +foreign import arcImpl :: EffectFn2 Context2D Arc Unit + -- | Render an arc object. -foreign import arc :: Context2D -> Arc -> Effect Unit +arc :: Context2D -> Arc -> Effect Unit +arc ctx a = runEffectFn2 arcImpl ctx a -- | A type representing a rectangle object: -- | @@ -440,17 +510,29 @@ type Rectangle = , height :: Number } +foreign import rectImpl :: EffectFn2 Context2D Rectangle Unit + -- | Render a rectangle. -foreign import rect :: Context2D -> Rectangle -> Effect Unit +rect :: Context2D -> Rectangle -> Effect Unit +rect ctx r = runEffectFn2 rectImpl ctx r + +foreign import fillRectImpl :: EffectFn2 Context2D Rectangle Unit -- | Fill a rectangle. -foreign import fillRect :: Context2D -> Rectangle -> Effect Unit +fillRect :: Context2D -> Rectangle -> Effect Unit +fillRect ctx r = runEffectFn2 fillRectImpl ctx r + +foreign import strokeRectImpl :: EffectFn2 Context2D Rectangle Unit -- | Stroke a rectangle. -foreign import strokeRect :: Context2D -> Rectangle -> Effect Unit +strokeRect :: Context2D -> Rectangle -> Effect Unit +strokeRect ctx r = runEffectFn2 strokeRectImpl ctx r + +foreign import clearRectImpl :: EffectFn2 Context2D Rectangle Unit -- | Clear a rectangle. -foreign import clearRect :: Context2D -> Rectangle -> Effect Unit +clearRect :: Context2D -> Rectangle -> Effect Unit +clearRect ctx r = runEffectFn2 clearRectImpl ctx r -- | An object representing a scaling transform: -- | @@ -460,11 +542,17 @@ type ScaleTransform = , scaleY :: Number } +foreign import scaleImpl :: EffectFn2 Context2D ScaleTransform Unit + -- | Apply a scaling transform. -foreign import scale :: Context2D -> ScaleTransform -> Effect Unit +scale :: Context2D -> ScaleTransform -> Effect Unit +scale ctx tr = runEffectFn2 scaleImpl ctx tr + +foreign import rotateImpl :: EffectFn2 Context2D Number Unit -- | Apply a rotation. -foreign import rotate :: Context2D -> Number -> Effect Unit +rotate :: Context2D -> Number -> Effect Unit +rotate ctx angle = runEffectFn2 rotateImpl ctx angle -- | An object representing a translation: -- | @@ -474,8 +562,11 @@ type TranslateTransform = , translateY :: Number } +foreign import translateImpl :: EffectFn2 Context2D TranslateTransform Unit + -- | Apply a translation -foreign import translate :: Context2D -> TranslateTransform -> Effect Unit +translate :: Context2D -> TranslateTransform -> Effect Unit +translate ctx tr = runEffectFn2 translateImpl ctx tr -- | An object representing a general transformation as a homogeneous matrix. type Transform = @@ -487,11 +578,18 @@ type Transform = , f :: Number } +foreign import transformImpl :: EffectFn2 Context2D Transform Unit + -- | Apply a general transformation to the current transformation matrix -foreign import transform :: Context2D -> Transform -> Effect Unit +transform :: Context2D -> Transform -> Effect Unit +transform ctx tr = runEffectFn2 transformImpl ctx tr + + +foreign import setTransformImpl :: EffectFn2 Context2D Transform Unit -- | Set the transformation matrix -foreign import setTransform :: Context2D -> Transform -> Effect Unit +setTransform :: Context2D -> Transform -> Effect Unit +setTransform ctx tr = runEffectFn2 setTransformImpl ctx tr -- | Enumerates types of text alignment. data TextAlign @@ -506,11 +604,11 @@ instance showTextAlign :: Show TextAlign where show AlignStart = "AlignStart" show AlignEnd = "AlignEnd" -foreign import textAlignImpl :: Context2D -> Effect String +foreign import textAlignImpl :: EffectFn1 Context2D String -- | Get the current text alignment. textAlign :: Context2D -> Effect TextAlign -textAlign ctx = unsafeParseTextAlign <$> textAlignImpl ctx +textAlign ctx = unsafeParseTextAlign <$> runEffectFn1 textAlignImpl ctx where unsafeParseTextAlign :: String -> TextAlign unsafeParseTextAlign "left" = AlignLeft @@ -521,12 +619,12 @@ textAlign ctx = unsafeParseTextAlign <$> textAlignImpl ctx unsafeParseTextAlign align = unsafeThrow $ "invalid TextAlign: " <> align -- ^ dummy to silence compiler warnings -foreign import setTextAlignImpl :: Context2D -> String -> Effect Unit +foreign import setTextAlignImpl :: EffectFn2 Context2D String Unit -- | Set the current text alignment. setTextAlign :: Context2D -> TextAlign -> Effect Unit setTextAlign ctx textalign = - setTextAlignImpl ctx (toString textalign) + runEffectFn2 setTextAlignImpl ctx (toString textalign) where toString AlignLeft = "left" toString AlignRight = "right" @@ -551,11 +649,11 @@ instance showTextBaseline :: Show TextBaseline where show BaselineIdeographic = "BaselineIdeographic" show BaselineBottom = "BaselineBottom" -foreign import textBaselineImpl :: Context2D -> Effect String +foreign import textBaselineImpl :: EffectFn1 Context2D String -- | Get the current text baseline. textBaseline :: Context2D -> Effect TextBaseline -textBaseline ctx = unsafeParseTextBaseline <$> textBaselineImpl ctx +textBaseline ctx = unsafeParseTextBaseline <$> runEffectFn1 textBaselineImpl ctx where unsafeParseTextBaseline :: String -> TextBaseline unsafeParseTextBaseline "top" = BaselineTop @@ -586,26 +684,48 @@ setTextBaseline ctx textbaseline = -- | - The text width in pixels. type TextMetrics = { width :: Number } +foreign import fontImpl :: EffectFn1 Context2D String + -- | Get the current font. -foreign import font :: Context2D -> Effect String +font :: Context2D -> Effect String +font ctx = runEffectFn1 fontImpl ctx + +foreign import setFontImpl :: EffectFn2 Context2D String Unit -- | Set the current font. -foreign import setFont :: Context2D -> String -> Effect Unit +setFont :: Context2D -> String -> Effect Unit +setFont ctx f = runEffectFn2 setFontImpl ctx f + + +foreign import fillTextImpl :: EffectFn4 Context2D String Number Number Unit -- | Fill some text. -foreign import fillText :: Context2D -> String -> Number -> Number -> Effect Unit +fillText :: Context2D -> String -> Number -> Number -> Effect Unit +fillText ctx text x y = runEffectFn4 fillTextImpl ctx text x y + +foreign import strokeTextImpl :: EffectFn4 Context2D String Number Number Unit -- | Stroke some text. -foreign import strokeText :: Context2D -> String -> Number -> Number -> Effect Unit +strokeText :: Context2D -> String -> Number -> Number -> Effect Unit +strokeText ctx text x y = runEffectFn4 strokeTextImpl ctx text x y + +foreign import measureTextImpl :: EffectFn2 Context2D String TextMetrics -- | Measure some text. -foreign import measureText :: Context2D -> String -> Effect TextMetrics +measureText :: Context2D -> String -> Effect TextMetrics +measureText ctx text = runEffectFn2 measureTextImpl ctx text + +foreign import saveImpl :: EffectFn1 Context2D Unit -- | Save the current context. -foreign import save :: Context2D -> Effect Unit +save :: Context2D -> Effect Unit +save ctx = runEffectFn1 saveImpl ctx + +foreign import restoreImpl :: EffectFn1 Context2D Unit -- | Restore the previous context. -foreign import restore :: Context2D -> Effect Unit +restore :: Context2D -> Effect Unit +restore ctx = runEffectFn1 restoreImpl ctx -- | A convenience function: run the action, preserving the existing context. -- | @@ -623,24 +743,42 @@ withContext ctx action = do _ <- restore ctx pure a +foreign import getImageDataImpl :: EffectFn5 Context2D Number Number Number Number ImageData + -- | Get image data for a portion of the canvas. -foreign import getImageData :: Context2D -> Number -> Number -> Number -> Number -> Effect ImageData +getImageData :: Context2D -> Number -> Number -> Number -> Number -> Effect ImageData +getImageData ctx x y w h = runEffectFn5 getImageDataImpl ctx x y w h + +foreign import putImageDataFullImpl :: EffectFn8 Context2D ImageData Number Number Number Number Number Number Unit -- | Set image data for a portion of the canvas. -foreign import putImageDataFull :: Context2D -> ImageData -> Number -> Number -> Number -> Number -> Number -> Number -> Effect Unit +putImageDataFull :: Context2D -> ImageData -> Number -> Number -> Number -> Number -> Number -> Number -> Effect Unit +putImageDataFull ctx data_ x y dx dy dw dh = runEffectFn8 putImageDataFullImpl ctx data_ x y dx dy dw dh + +foreign import putImageDataImpl :: EffectFn4 Context2D ImageData Number Number Unit -- | Set image data for a portion of the canvas. -foreign import putImageData :: Context2D -> ImageData -> Number -> Number -> Effect Unit +putImageData :: Context2D -> ImageData -> Number -> Number -> Effect Unit +putImageData ctx data_ x y = runEffectFn4 putImageDataImpl ctx data_ x y + +foreign import createImageDataImpl :: EffectFn3 Context2D Number Number ImageData -- | Create an image data object. -foreign import createImageData :: Context2D -> Number -> Number -> Effect ImageData +createImageData :: Context2D -> Number -> Number -> Effect ImageData +createImageData ctx sw sh = runEffectFn3 createImageDataImpl ctx sw sh + +foreign import createImageDataCopyImpl :: EffectFn2 Context2D ImageData ImageData -- | Create a copy of an image data object. -foreign import createImageDataCopy :: Context2D -> ImageData -> Effect ImageData +createImageDataCopy :: Context2D -> ImageData -> Effect ImageData +createImageDataCopy ctx img = runEffectFn2 createImageDataCopyImpl ctx img + +foreign import createImageDataWithImpl :: EffectFn2 Uint8ClampedArray Int ImageData -- | Create an image data object given a `Uint8ClampedArray` containing the underlying pixel representation of the image. -- | The height is inferred from the array's size and the given width. -foreign import createImageDataWith :: Uint8ClampedArray -> Int -> Effect ImageData +createImageDataWith :: Uint8ClampedArray -> Int -> Effect ImageData +createImageDataWith arr sw = runEffectFn2 createImageDataWithImpl arr sw -- | Get the width of an `ImageData` object. foreign import imageDataWidth :: ImageData -> Int @@ -651,11 +789,20 @@ foreign import imageDataHeight :: ImageData -> Int -- | Get the underlying buffer from an `ImageData` object. foreign import imageDataBuffer :: ImageData -> Uint8ClampedArray -foreign import drawImage :: Context2D -> CanvasImageSource -> Number -> Number -> Effect Unit +foreign import drawImageImpl :: EffectFn4 Context2D CanvasImageSource Number Number Unit + +drawImage :: Context2D -> CanvasImageSource -> Number -> Number -> Effect Unit +drawImage ctx source dx dy = runEffectFn4 drawImageImpl ctx source dx dy + +foreign import drawImageScaleImpl :: EffectFn6 Context2D CanvasImageSource Number Number Number Number Unit + +drawImageScale :: Context2D -> CanvasImageSource -> Number -> Number -> Number -> Number -> Effect Unit +drawImageScale ctx source dx dy dw dh = runEffectFn6 drawImageScaleImpl ctx source dx dy dw dh -foreign import drawImageScale :: Context2D -> CanvasImageSource -> Number -> Number -> Number -> Number -> Effect Unit +foreign import drawImageFullImpl :: EffectFn10 Context2D CanvasImageSource Number Number Number Number Number Number Number Number Unit -foreign import drawImageFull :: Context2D -> CanvasImageSource -> Number -> Number -> Number -> Number -> Number -> Number -> Number -> Number -> Effect Unit +drawImageFull :: Context2D -> CanvasImageSource -> Number -> Number -> Number -> Number -> Number -> Number -> Number -> Number -> Effect Unit +drawImageFull ctx source sx sy sw sh dx dy dw dh = runEffectFn10 drawImageFullImpl ctx source sx sy sw sh dx dy dw dh -- | Enumerates the different types of pattern repetitions. data PatternRepeat = Repeat | RepeatX | RepeatY | NoRepeat @@ -668,19 +815,22 @@ instance showPatternRepeat :: Show PatternRepeat where show RepeatY = "RepeatY" show NoRepeat = "NoRepeat" -foreign import createPatternImpl :: Context2D -> CanvasImageSource -> String -> Effect CanvasPattern +foreign import createPatternImpl :: EffectFn3 Context2D CanvasImageSource String CanvasPattern -- | Create a new canvas pattern (repeatable image). createPattern :: Context2D -> CanvasImageSource -> PatternRepeat -> Effect CanvasPattern -createPattern context img repeat = createPatternImpl context img (toString repeat) +createPattern context img repeat = runEffectFn3 createPatternImpl context img (toString repeat) where toString Repeat = "repeat" toString RepeatX = "repeat-x" toString RepeatY = "repeat-y" toString NoRepeat = "no-repeat" +foreign import setPatternFillStyleImpl :: EffectFn2 Context2D CanvasPattern Unit + -- | Set the Context2D fillstyle to the CanvasPattern. -foreign import setPatternFillStyle :: Context2D -> CanvasPattern -> Effect Unit +setPatternFillStyle :: Context2D -> CanvasPattern -> Effect Unit +setPatternFillStyle ctx pattern = runEffectFn2 setPatternFillStyleImpl ctx pattern -- | A type representing a linear gradient. -- | - Starting point coordinates: (`x0`, `y0`) @@ -693,8 +843,11 @@ type LinearGradient = , y1 :: Number } +foreign import createLinearGradientImpl :: EffectFn2 Context2D LinearGradient CanvasGradient + -- | Create a linear CanvasGradient. -foreign import createLinearGradient :: Context2D -> LinearGradient -> Effect CanvasGradient +createLinearGradient :: Context2D -> LinearGradient -> Effect CanvasGradient +createLinearGradient ctx grad = runEffectFn2 createLinearGradientImpl ctx grad -- | A type representing a radial gradient. -- | - Starting circle center coordinates: (`x0`, `y0`) @@ -711,14 +864,23 @@ type RadialGradient = , r1 :: Number } +foreign import createRadialGradientImpl :: EffectFn2 Context2D RadialGradient CanvasGradient + -- | Create a radial CanvasGradient. -foreign import createRadialGradient :: Context2D -> RadialGradient -> Effect CanvasGradient +createRadialGradient :: Context2D -> RadialGradient -> Effect CanvasGradient +createRadialGradient ctx grad = runEffectFn2 createRadialGradientImpl ctx grad + +foreign import addColorStopImpl :: EffectFn3 CanvasGradient Number String Unit -- | Add a single color stop to a CanvasGradient. -foreign import addColorStop :: CanvasGradient -> Number -> String -> Effect Unit +addColorStop :: CanvasGradient -> Number -> String -> Effect Unit +addColorStop grad stop color = runEffectFn3 addColorStopImpl grad stop color + +foreign import setGradientFillStyleImpl :: EffectFn2 Context2D CanvasGradient Unit -- | Set the Context2D fillstyle to the CanvasGradient. -foreign import setGradientFillStyle :: Context2D -> CanvasGradient -> Effect Unit +setGradientFillStyle :: Context2D -> CanvasGradient -> Effect Unit +setGradientFillStyle ctx gradient = runEffectFn2 setGradientFillStyleImpl ctx gradient -- | A type representing a quadratic Bézier curve. -- | - Bézier control point: (`cpx`, `cpy`) @@ -731,8 +893,11 @@ type QuadraticCurve = , y :: Number } +foreign import quadraticCurveToImpl :: EffectFn2 Context2D QuadraticCurve Unit + -- | Draw a quadratic Bézier curve. -foreign import quadraticCurveTo :: Context2D -> QuadraticCurve -> Effect Unit +quadraticCurveTo :: Context2D -> QuadraticCurve -> Effect Unit +quadraticCurveTo ctx curve = runEffectFn2 quadraticCurveToImpl ctx curve -- | A type representing a cubic Bézier curve. -- | - First Bézier control point: (`cp1x`, `cp1y`) @@ -748,5 +913,8 @@ type BezierCurve = , y :: Number } +foreign import bezierCurveToImpl :: EffectFn2 Context2D BezierCurve Unit + -- | Draw a cubic Bézier curve. -foreign import bezierCurveTo :: Context2D -> BezierCurve -> Effect Unit +bezierCurveTo :: Context2D -> BezierCurve -> Effect Unit +bezierCurveTo ctx curve = runEffectFn2 bezierCurveToImpl ctx curve \ No newline at end of file From 83cd98ab7be8d4cc1d520a860cdba92789633e18 Mon Sep 17 00:00:00 2001 From: Guillaume Bagan Date: Wed, 10 May 2023 22:29:48 +0200 Subject: [PATCH 2/7] remove non necessary dependency to console --- spago.dhall | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spago.dhall b/spago.dhall index 853c48e..bae4f7e 100644 --- a/spago.dhall +++ b/spago.dhall @@ -1,6 +1,6 @@ { name = "canvas" , dependencies = - [ "arraybuffer-types", "console", "effect", "exceptions", "maybe", "prelude" ] + [ "arraybuffer-types", "effect", "exceptions", "maybe", "prelude" ] , packages = ./packages.dhall , sources = [ "src/**/*.purs" ] } From c050a0f3da020eda3ff973fd06d15fd2f58f5187 Mon Sep 17 00:00:00 2001 From: Guillaume Bagan Date: Wed, 10 May 2023 23:00:15 +0200 Subject: [PATCH 3/7] update package.json --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index bd93a90..b9e27d2 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "private": true, "scripts": { - "clean": "rimraf output && rimraf .pulp-cache", + "clean": "rimraf output && rimraf .spago", "build": "eslint src && spago build" }, "devDependencies": { - "eslint": "^7.32.0", + "eslint": "^8.40.0", "purescript-psa": "^0.8.2", - "rimraf": "^3.0.2", + "rimraf": "^5.0.0", "spago": "^0.21.0" } } From c1d56f86fb2fe53a4c774968a459de457d023a17 Mon Sep 17 00:00:00 2001 From: Guillaume Bagan Date: Wed, 10 May 2023 23:22:58 +0200 Subject: [PATCH 4/7] forgot to uncurry setLineCapImpl and setLineJoinImpl --- src/Graphics/Canvas.purs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Graphics/Canvas.purs b/src/Graphics/Canvas.purs index b04a1ab..47fa450 100644 --- a/src/Graphics/Canvas.purs +++ b/src/Graphics/Canvas.purs @@ -275,13 +275,13 @@ data LineCap = Round | Square | Butt derive instance eqLineCap :: Eq LineCap -foreign import setLineCapImpl :: Context2D -> String -> Effect Unit +foreign import setLineCapImpl :: EffectFn2 Context2D String Unit -- | Set the current line cap type. setLineCap :: Context2D -> LineCap -> Effect Unit -setLineCap context Round = setLineCapImpl context "round" -setLineCap context Square = setLineCapImpl context "square" -setLineCap context Butt = setLineCapImpl context "butt" +setLineCap context Round = runEffectFn2 setLineCapImpl context "round" +setLineCap context Square = runEffectFn2 setLineCapImpl context "square" +setLineCap context Butt = runEffectFn2 setLineCapImpl context "butt" -- Note that we can't re-use `Round` from LineCap, so I've added `Join` to all of these @@ -290,13 +290,13 @@ data LineJoin = BevelJoin | RoundJoin | MiterJoin derive instance eqLineJoin :: Eq LineJoin -foreign import setLineJoinImpl :: Context2D -> String -> Effect Unit +foreign import setLineJoinImpl :: EffectFn2 Context2D String Unit -- | Set the current line join type. setLineJoin :: Context2D -> LineJoin -> Effect Unit -setLineJoin context BevelJoin = setLineJoinImpl context "bevel" -setLineJoin context RoundJoin = setLineJoinImpl context "round" -setLineJoin context MiterJoin = setLineJoinImpl context "miter" +setLineJoin context BevelJoin = runEffectFn2 setLineJoinImpl context "bevel" +setLineJoin context RoundJoin = runEffectFn2 setLineJoinImpl context "round" +setLineJoin context MiterJoin = runEffectFn2 setLineJoinImpl context "miter" -- | Enumerates the different types of composite operations and blend modes. data Composite @@ -665,12 +665,12 @@ textBaseline ctx = unsafeParseTextBaseline <$> runEffectFn1 textBaselineImpl ctx unsafeParseTextBaseline align = unsafeThrow $ "invalid TextBaseline: " <> align -- ^ dummy to silence compiler warnings -foreign import setTextBaselineImpl :: Context2D -> String -> Effect Unit +foreign import setTextBaselineImpl :: EffectFn2 Context2D String Unit -- | Set the current text baseline. setTextBaseline :: Context2D -> TextBaseline -> Effect Unit setTextBaseline ctx textbaseline = - setTextBaselineImpl ctx (toString textbaseline) + runEffectFn2 setTextBaselineImpl ctx (toString textbaseline) where toString BaselineTop = "top" toString BaselineHanging = "hanging" From 2978ac4d9bf92f0b270d80064137f0ef5a3804df Mon Sep 17 00:00:00 2001 From: Guillaume Bagan Date: Wed, 10 May 2023 23:39:47 +0200 Subject: [PATCH 5/7] fix a typo --- src/Graphics/Canvas.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Graphics/Canvas.purs b/src/Graphics/Canvas.purs index 47fa450..408cfc7 100644 --- a/src/Graphics/Canvas.purs +++ b/src/Graphics/Canvas.purs @@ -260,7 +260,7 @@ setShadowOffsetX ctx x = runEffectFn2 setShadowOffsetXImpl ctx x foreign import setShadowOffsetYImpl :: EffectFn2 Context2D Number Unit --- | Set the current shadow x-offset. +-- | Set the current shadow y-offset. setShadowOffsetY :: Context2D -> Number -> Effect Unit setShadowOffsetY ctx y = runEffectFn2 setShadowOffsetYImpl ctx y From 7875c246f8b8299b84dc32760ff5f07b50009a54 Mon Sep 17 00:00:00 2001 From: Guillaume Bagan Date: Wed, 10 May 2023 23:55:54 +0200 Subject: [PATCH 6/7] use purs-tidy and update ci.yml --- .github/workflows/ci.yml | 22 +-- src/Graphics/Canvas.purs | 283 +++++++++++++++++++++------------------ 2 files changed, 162 insertions(+), 143 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 06ed895..8d7968d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,21 +15,21 @@ jobs: - uses: purescript-contrib/setup-purescript@main with: purescript: "unstable" + purs-tidy: "latest" - - uses: actions/setup-node@v2 + - name: Cache PureScript dependencies + uses: actions/cache@v2 with: - node-version: "14" + key: ${{ runner.os }}-spago-${{ hashFiles('**/*.dhall') }} + path: | + .spago + output - name: Install dependencies - run: | - npm install -g bower - npm install - bower install --production + run: spago install - name: Build source - run: npm run-script build + run: spago build --no-install --purs-args '--censor-lib --strict' - - name: Run tests - run: | - bower install - npm run-script test --if-present + - name: Check formatting + run: purs-tidy check src \ No newline at end of file diff --git a/src/Graphics/Canvas.purs b/src/Graphics/Canvas.purs index 408cfc7..ca5dbff 100644 --- a/src/Graphics/Canvas.purs +++ b/src/Graphics/Canvas.purs @@ -119,8 +119,24 @@ module Graphics.Canvas import Prelude import Effect (Effect) -import Effect.Uncurried ( EffectFn1, EffectFn2, EffectFn3, EffectFn4, EffectFn5, EffectFn6, EffectFn8, EffectFn10 - , runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4, runEffectFn5, runEffectFn6, runEffectFn8, runEffectFn10) +import Effect.Uncurried + ( EffectFn1 + , EffectFn2 + , EffectFn3 + , EffectFn4 + , EffectFn5 + , EffectFn6 + , EffectFn8 + , EffectFn10 + , runEffectFn1 + , runEffectFn2 + , runEffectFn3 + , runEffectFn4 + , runEffectFn5 + , runEffectFn6 + , runEffectFn8 + , runEffectFn10 + ) import Effect.Exception.Unsafe (unsafeThrow) import Data.ArrayBuffer.Types (Uint8ClampedArray) import Data.Maybe (Maybe(..)) @@ -165,7 +181,6 @@ foreign import getCanvasElementByIdImpl getCanvasElementById :: String -> Effect (Maybe CanvasElement) getCanvasElementById elId = runEffectFn3 getCanvasElementByIdImpl elId Just Nothing - foreign import getContext2DImpl :: EffectFn1 CanvasElement Context2D -- | Get the 2D graphics context for a canvas element. @@ -202,9 +217,9 @@ type Dimensions = { width :: Number, height :: Number } -- | Get the canvas dimensions in pixels. getCanvasDimensions :: CanvasElement -> Effect Dimensions getCanvasDimensions ce = do - w <- getCanvasWidth ce + w <- getCanvasWidth ce h <- getCanvasHeight ce - pure {width : w, height : h} + pure { width: w, height: h } -- | Set the canvas dimensions in pixels. setCanvasDimensions :: CanvasElement -> Dimensions -> Effect Unit @@ -244,13 +259,13 @@ foreign import setShadowColorImpl :: EffectFn2 Context2D String Unit -- | Set the current shadow color. setShadowColor :: Context2D -> String -> Effect Unit -setShadowColor ctx color = runEffectFn2 setShadowColorImpl ctx color +setShadowColor ctx color = runEffectFn2 setShadowColorImpl ctx color foreign import setShadowBlurImpl :: EffectFn2 Context2D Number Unit -- | Set the current shadow blur radius. setShadowBlur :: Context2D -> Number -> Effect Unit -setShadowBlur ctx radius = runEffectFn2 setShadowBlurImpl ctx radius +setShadowBlur ctx radius = runEffectFn2 setShadowBlurImpl ctx radius foreign import setShadowOffsetXImpl :: EffectFn2 Context2D Number Unit @@ -279,9 +294,9 @@ foreign import setLineCapImpl :: EffectFn2 Context2D String Unit -- | Set the current line cap type. setLineCap :: Context2D -> LineCap -> Effect Unit -setLineCap context Round = runEffectFn2 setLineCapImpl context "round" +setLineCap context Round = runEffectFn2 setLineCapImpl context "round" setLineCap context Square = runEffectFn2 setLineCapImpl context "square" -setLineCap context Butt = runEffectFn2 setLineCapImpl context "butt" +setLineCap context Butt = runEffectFn2 setLineCapImpl context "butt" -- Note that we can't re-use `Round` from LineCap, so I've added `Join` to all of these @@ -301,64 +316,64 @@ setLineJoin context MiterJoin = runEffectFn2 setLineJoinImpl context "miter" -- | Enumerates the different types of composite operations and blend modes. data Composite -- Composite Operations - = SourceOver - | SourceIn - | SourceOut - | SourceAtop - | DestinationOver - | DestinationIn - | DestinationOut - | DestinationAtop - | Lighter - | Copy - | Xor - - -- Blend Modes - | Multiply - | Screen - | Overlay - | Darken - | Lighten - | ColorDodge - | ColorBurn - | HardLight - | SoftLight - | Difference - | Exclusion - | Hue - | Saturation - | Color - | Luminosity + = SourceOver + | SourceIn + | SourceOut + | SourceAtop + | DestinationOver + | DestinationIn + | DestinationOut + | DestinationAtop + | Lighter + | Copy + | Xor + + -- Blend Modes + | Multiply + | Screen + | Overlay + | Darken + | Lighten + | ColorDodge + | ColorBurn + | HardLight + | SoftLight + | Difference + | Exclusion + | Hue + | Saturation + | Color + | Luminosity derive instance eqComposite :: Eq Composite instance showComposite :: Show Composite where - show SourceOver = "SourceOver" - show SourceIn = "SourceIn" - show SourceOut = "SourceOut" - show SourceAtop = "SourceAtop" + show SourceOver = "SourceOver" + show SourceIn = "SourceIn" + show SourceOut = "SourceOut" + show SourceAtop = "SourceAtop" show DestinationOver = "DestinationOver" - show DestinationIn = "DestinationIn" - show DestinationOut = "DestinationOut" + show DestinationIn = "DestinationIn" + show DestinationOut = "DestinationOut" show DestinationAtop = "DestinationAtop" - show Lighter = "Lighter" - show Copy = "Copy" - show Xor = "Xor" - show Multiply = "Multiply" - show Screen = "Screen" - show Overlay = "Overlay" - show Darken = "Darken" - show Lighten = "Lighten" - show ColorDodge = "ColorDodge" - show ColorBurn = "ColorBurn" - show HardLight = "HardLight" - show SoftLight = "SoftLight" - show Difference = "Difference" - show Exclusion = "Exclusion" - show Hue = "Hue" - show Saturation = "Saturation" - show Color = "Color" - show Luminosity = "Luminosity" + show Lighter = "Lighter" + show Copy = "Copy" + show Xor = "Xor" + show Multiply = "Multiply" + show Screen = "Screen" + show Overlay = "Overlay" + show Darken = "Darken" + show Lighten = "Lighten" + show ColorDodge = "ColorDodge" + show ColorBurn = "ColorBurn" + show HardLight = "HardLight" + show SoftLight = "SoftLight" + show Difference = "Difference" + show Exclusion = "Exclusion" + show Hue = "Hue" + show Saturation = "Saturation" + show Color = "Color" + show Luminosity = "Luminosity" foreign import setGlobalCompositeOperationImpl :: EffectFn2 Context2D String Unit @@ -366,32 +381,32 @@ foreign import setGlobalCompositeOperationImpl :: EffectFn2 Context2D String Uni setGlobalCompositeOperation :: Context2D -> Composite -> Effect Unit setGlobalCompositeOperation ctx composite = runEffectFn2 setGlobalCompositeOperationImpl ctx (toString composite) where - toString SourceOver = "source-over" - toString SourceIn = "source-in" - toString SourceOut = "source-out" - toString SourceAtop = "source-atop" - toString DestinationOver = "destination-over" - toString DestinationIn = "destination-in" - toString DestinationOut = "destination-out" - toString DestinationAtop = "destination-atop" - toString Lighter = "lighter" - toString Copy = "copy" - toString Xor = "xor" - toString Multiply = "multiply" - toString Screen = "screen" - toString Overlay = "overlay" - toString Darken = "darken" - toString Lighten = "lighten" - toString ColorDodge = "color-dodge" - toString ColorBurn = "color-burn" - toString HardLight = "hard-light" - toString SoftLight = "soft-light" - toString Difference = "difference" - toString Exclusion = "exclusion" - toString Hue = "hue" - toString Saturation = "saturation" - toString Color = "color" - toString Luminosity = "luminosity" + toString SourceOver = "source-over" + toString SourceIn = "source-in" + toString SourceOut = "source-out" + toString SourceAtop = "source-atop" + toString DestinationOver = "destination-over" + toString DestinationIn = "destination-in" + toString DestinationOut = "destination-out" + toString DestinationAtop = "destination-atop" + toString Lighter = "lighter" + toString Copy = "copy" + toString Xor = "xor" + toString Multiply = "multiply" + toString Screen = "screen" + toString Overlay = "overlay" + toString Darken = "darken" + toString Lighten = "lighten" + toString ColorDodge = "color-dodge" + toString ColorBurn = "color-burn" + toString HardLight = "hard-light" + toString SoftLight = "soft-light" + toString Difference = "difference" + toString Exclusion = "exclusion" + toString Hue = "hue" + toString Saturation = "saturation" + toString Color = "color" + toString Luminosity = "luminosity" foreign import setGlobalAlphaImpl :: EffectFn2 Context2D Number Unit @@ -489,7 +504,7 @@ type Arc = , y :: Number , radius :: Number , start :: Number - , end :: Number + , end :: Number , useCounterClockwise :: Boolean } @@ -584,7 +599,6 @@ foreign import transformImpl :: EffectFn2 Context2D Transform Unit transform :: Context2D -> Transform -> Effect Unit transform ctx tr = runEffectFn2 transformImpl ctx tr - foreign import setTransformImpl :: EffectFn2 Context2D Transform Unit -- | Set the transformation matrix @@ -593,7 +607,11 @@ setTransform ctx tr = runEffectFn2 setTransformImpl ctx tr -- | Enumerates types of text alignment. data TextAlign - = AlignLeft | AlignRight | AlignCenter | AlignStart | AlignEnd + = AlignLeft + | AlignRight + | AlignCenter + | AlignStart + | AlignEnd derive instance eqTextAlign :: Eq TextAlign @@ -617,7 +635,8 @@ textAlign ctx = unsafeParseTextAlign <$> runEffectFn1 textAlignImpl ctx unsafeParseTextAlign "start" = AlignStart unsafeParseTextAlign "end" = AlignEnd unsafeParseTextAlign align = unsafeThrow $ "invalid TextAlign: " <> align - -- ^ dummy to silence compiler warnings + +-- ^ dummy to silence compiler warnings foreign import setTextAlignImpl :: EffectFn2 Context2D String Unit @@ -626,11 +645,11 @@ setTextAlign :: Context2D -> TextAlign -> Effect Unit setTextAlign ctx textalign = runEffectFn2 setTextAlignImpl ctx (toString textalign) where - toString AlignLeft = "left" - toString AlignRight = "right" - toString AlignCenter = "center" - toString AlignStart = "start" - toString AlignEnd = "end" + toString AlignLeft = "left" + toString AlignRight = "right" + toString AlignCenter = "center" + toString AlignStart = "start" + toString AlignEnd = "end" -- | Enumerates types of text baseline. data TextBaseline @@ -663,7 +682,8 @@ textBaseline ctx = unsafeParseTextBaseline <$> runEffectFn1 textBaselineImpl ctx unsafeParseTextBaseline "ideographic" = BaselineIdeographic unsafeParseTextBaseline "bottom" = BaselineBottom unsafeParseTextBaseline align = unsafeThrow $ "invalid TextBaseline: " <> align - -- ^ dummy to silence compiler warnings + +-- ^ dummy to silence compiler warnings foreign import setTextBaselineImpl :: EffectFn2 Context2D String Unit @@ -672,12 +692,12 @@ setTextBaseline :: Context2D -> TextBaseline -> Effect Unit setTextBaseline ctx textbaseline = runEffectFn2 setTextBaselineImpl ctx (toString textbaseline) where - toString BaselineTop = "top" - toString BaselineHanging = "hanging" - toString BaselineMiddle = "middle" - toString BaselineAlphabetic = "alphabetic" - toString BaselineIdeographic = "ideographic" - toString BaselineBottom = "bottom" + toString BaselineTop = "top" + toString BaselineHanging = "hanging" + toString BaselineMiddle = "middle" + toString BaselineAlphabetic = "alphabetic" + toString BaselineIdeographic = "ideographic" + toString BaselineBottom = "bottom" -- | Text metrics: -- | @@ -696,7 +716,6 @@ foreign import setFontImpl :: EffectFn2 Context2D String Unit setFont :: Context2D -> String -> Effect Unit setFont ctx f = runEffectFn2 setFontImpl ctx f - foreign import fillTextImpl :: EffectFn4 Context2D String Number Number Unit -- | Fill some text. @@ -821,10 +840,10 @@ foreign import createPatternImpl :: EffectFn3 Context2D CanvasImageSource String createPattern :: Context2D -> CanvasImageSource -> PatternRepeat -> Effect CanvasPattern createPattern context img repeat = runEffectFn3 createPatternImpl context img (toString repeat) where - toString Repeat = "repeat" - toString RepeatX = "repeat-x" - toString RepeatY = "repeat-y" - toString NoRepeat = "no-repeat" + toString Repeat = "repeat" + toString RepeatX = "repeat-x" + toString RepeatY = "repeat-y" + toString NoRepeat = "no-repeat" foreign import setPatternFillStyleImpl :: EffectFn2 Context2D CanvasPattern Unit @@ -837,11 +856,11 @@ setPatternFillStyle ctx pattern = runEffectFn2 setPatternFillStyleImpl ctx patte -- | - Ending point coordinates: (`x1`, `y1`) type LinearGradient = - { x0 :: Number - , y0 :: Number - , x1 :: Number - , y1 :: Number - } + { x0 :: Number + , y0 :: Number + , x1 :: Number + , y1 :: Number + } foreign import createLinearGradientImpl :: EffectFn2 Context2D LinearGradient CanvasGradient @@ -856,13 +875,13 @@ createLinearGradient ctx grad = runEffectFn2 createLinearGradientImpl ctx grad -- | - Ending circle radius: `r1` type RadialGradient = - { x0 :: Number - , y0 :: Number - , r0 :: Number - , x1 :: Number - , y1 :: Number - , r1 :: Number - } + { x0 :: Number + , y0 :: Number + , r0 :: Number + , x1 :: Number + , y1 :: Number + , r1 :: Number + } foreign import createRadialGradientImpl :: EffectFn2 Context2D RadialGradient CanvasGradient @@ -887,11 +906,11 @@ setGradientFillStyle ctx gradient = runEffectFn2 setGradientFillStyleImpl ctx gr -- | - Ending point coordinates: (`x`, `y`) type QuadraticCurve = - { cpx :: Number - , cpy :: Number - , x :: Number - , y :: Number - } + { cpx :: Number + , cpy :: Number + , x :: Number + , y :: Number + } foreign import quadraticCurveToImpl :: EffectFn2 Context2D QuadraticCurve Unit @@ -905,13 +924,13 @@ quadraticCurveTo ctx curve = runEffectFn2 quadraticCurveToImpl ctx curve -- | - Ending point: (`x`, `y`) type BezierCurve = - { cp1x :: Number - , cp1y :: Number - , cp2x :: Number - , cp2y :: Number - , x :: Number - , y :: Number - } + { cp1x :: Number + , cp1y :: Number + , cp2x :: Number + , cp2y :: Number + , x :: Number + , y :: Number + } foreign import bezierCurveToImpl :: EffectFn2 Context2D BezierCurve Unit From e022d3f20aaf0c069473d7a685811764652c76f3 Mon Sep 17 00:00:00 2001 From: Guillaume Bagan Date: Thu, 11 May 2023 08:34:22 +0200 Subject: [PATCH 7/7] update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e125bde..0b12dc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ New features: Bugfixes: Other improvements: +- Update FFI to EffectFnX types (#90 by @gbagan) ## [v6.0.0](https://github.com/purescript-web/purescript-canvas/releases/tag/v6.0.0) - 2022-04-27