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

Added missing constructors to ImageData facade. #829

Merged
merged 13 commits into from
Mar 21, 2024
6 changes: 5 additions & 1 deletion api-reports/2_12.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15838,9 +15838,10 @@ ImageBitmap[JT] def width: Double
ImageCapture[JC] def grabFrame(): js.Promise[ImageBitmap]
ImageCapture[JC] def takePhoto(): js.Promise[Blob]
ImageCapture[JC] val track: MediaStreamTrack
ImageData[JC] def data: js.typedarray.Uint8ClampedArray
ImageData[JC] def data: Uint8ClampedArray
ImageData[JC] def height: Int
ImageData[JC] def width: Int
ImageDataSettings[JT] var colorSpace: js.UndefOr[PredefinedColorSpace]
InputEvent[JC] def bubbles: Boolean
InputEvent[JC] def cancelBubble: Boolean
InputEvent[JC] def cancelable: Boolean
Expand Down Expand Up @@ -17397,6 +17398,9 @@ PositionError[JT] def message: String
PositionOptions[JC] var enableHighAccuracy: Boolean
PositionOptions[JC] var maximumAge: Int
PositionOptions[JC] var timeout: Int
PredefinedColorSpace[JT]
PredefinedColorSpace[SO] val `display-p3`: PredefinedColorSpace
PredefinedColorSpace[SO] val srgb: PredefinedColorSpace
PresentationStyle[JT]
PresentationStyle[SO] val attachment: PresentationStyle
PresentationStyle[SO] val inline: PresentationStyle
Expand Down
6 changes: 5 additions & 1 deletion api-reports/2_13.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15838,9 +15838,10 @@ ImageBitmap[JT] def width: Double
ImageCapture[JC] def grabFrame(): js.Promise[ImageBitmap]
ImageCapture[JC] def takePhoto(): js.Promise[Blob]
ImageCapture[JC] val track: MediaStreamTrack
ImageData[JC] def data: js.typedarray.Uint8ClampedArray
ImageData[JC] def data: Uint8ClampedArray
ImageData[JC] def height: Int
ImageData[JC] def width: Int
ImageDataSettings[JT] var colorSpace: js.UndefOr[PredefinedColorSpace]
InputEvent[JC] def bubbles: Boolean
InputEvent[JC] def cancelBubble: Boolean
InputEvent[JC] def cancelable: Boolean
Expand Down Expand Up @@ -17397,6 +17398,9 @@ PositionError[JT] def message: String
PositionOptions[JC] var enableHighAccuracy: Boolean
PositionOptions[JC] var maximumAge: Int
PositionOptions[JC] var timeout: Int
PredefinedColorSpace[JT]
PredefinedColorSpace[SO] val `display-p3`: PredefinedColorSpace
PredefinedColorSpace[SO] val srgb: PredefinedColorSpace
PresentationStyle[JT]
PresentationStyle[SO] val attachment: PresentationStyle
PresentationStyle[SO] val inline: PresentationStyle
Expand Down
12 changes: 12 additions & 0 deletions dom/src/main/scala-2/org/scalajs/dom/PredefinedColorSpace.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.scalajs.dom

import scala.scalajs.js

@js.native
sealed trait PredefinedColorSpace extends js.Any

object PredefinedColorSpace {
val srgb: PredefinedColorSpace = "srgb".asInstanceOf[PredefinedColorSpace]

val `display-p3`: PredefinedColorSpace = "display-p3".asInstanceOf[PredefinedColorSpace]
}
11 changes: 11 additions & 0 deletions dom/src/main/scala-3/org/scalajs/dom/PredefinedColorSpace.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.scalajs.dom

import scala.scalajs.js

opaque type PredefinedColorSpace <: String = String

object PredefinedColorSpace {
val srgb: PredefinedColorSpace = "srgb"

val `display-p3`: PredefinedColorSpace = "display-p3"
}
52 changes: 51 additions & 1 deletion dom/src/main/scala/org/scalajs/dom/ImageData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package org.scalajs.dom

import scala.scalajs.js
import scala.scalajs.js.annotation._
import scala.scalajs.js.typedarray.Uint8ClampedArray

/** The ImageData interface represents the underlying pixel data of an area of a &lt;canvas&gt; element. It is created
* using creators on the CanvasRenderingContext2D object associated with the canvas createImageData() and
Expand All @@ -17,14 +18,63 @@ import scala.scalajs.js.annotation._
@JSGlobal
class ImageData extends js.Object {

/** Create an ImageData instance from an array of pixel data and a width.
* @param data
* pixel data
* @param width
* width in pixels
*/
def this(data: Uint8ClampedArray, width: Int) = this()

/** Create an ImageData instance from an array of pixel data, width, and height.
* @param data
* pixel data
* @param width
* width in pixels
* @param height
* height in pixels
*/
def this(data: Uint8ClampedArray, width: Int, height: Int) = this()

/** Create a blank ImageData instance from specified width and height.
* @param width
* width in pixels
* @param height
* height in pixels
*/
def this(width: Int, height: Int) = this()

/** Create a blank ImageData instance from specified width, height, and settings object.
* @param width
* width in pixels
* @param height
* height in pixels
* @param settings
* image settings
*/
def this(width: Int, height: Int, settings: ImageDataSettings) = this()

/** Create a blank ImageData instance from specified pixel data, width, height, and settings object.
* @param data
* pixel data
* @param width
* width in pixels
* @param height
* height in pixels
* @param settings
* image settings
*/
def this(data: Uint8ClampedArray, width: Int, height: Int, settings: ImageDataSettings) = this()

/** Is an unsigned long representing the actual width, in pixels, of the ImageData. */
def width: Int = js.native

/** Is a Uint8ClampedArray representing a one-dimensional array containing the data in the RGBA order, with integer
* values between 0 and 255 (included).
*/
def data: js.typedarray.Uint8ClampedArray = js.native
def data: Uint8ClampedArray = js.native

/** Is an unsigned long representing the actual height, in pixels, of the ImageData. */
def height: Int = js.native

}
7 changes: 7 additions & 0 deletions dom/src/main/scala/org/scalajs/dom/ImageDataSettings.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.scalajs.dom

import scala.scalajs.js

trait ImageDataSettings extends js.Object {
var colorSpace: js.UndefOr[PredefinedColorSpace] = js.undefined
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,44 @@ trait BrowserTests extends WebCryptoApiTests {
_ <- reasonPromise.future.map(assertEquals(expectedReason, _))
} yield ()
}

@Test
final def ImageDataTest(): Unit = {
// Test ImageDataConstructors
// from https://developer.mozilla.org/en-US/docs/Web/API/ImageData/ImageData

import org.scalajs.dom.{ImageData, ImageDataSettings, PredefinedColorSpace}
import PredefinedColorSpace._

val width:Int = 200
val height:Int = 100

// new ImageData(width, height)
val imgDat1: ImageData = new ImageData(width, height)
assertEquals(imgDat1.width, width)
assertEquals(imgDat1.height, height)
assertEquals(imgDat1.data.length, width * height * 4)

// new ImageData(width, height, settings) // Firefox doesn't support this constructor.
// val defaultImageData: ImageData = new ImageData(width, height, new ImageDataSettings { colorSpace = `display-p3` })
// assertEquals(defaultImageData.width, width)
// assertEquals(defaultImageData.height, height)

// new ImageData(dataArray, width)
val imgDat2: ImageData = new ImageData(imgDat1.data, width)
assertEquals(imgDat2.width, width)
assertEquals(imgDat2.height, height)
assertEquals(imgDat2.data.length, width * height * 4)

// new ImageData(dataArray, width, height)
val imgDat3: ImageData = new ImageData(imgDat2.data, width, height)
assertEquals(imgDat3.width, width)
assertEquals(imgDat3.height, height)
assertEquals(imgDat3.data.length, width * height * 4)

// new ImageData(dataArray, width, height, settings)
val defaultImageData: ImageData = new ImageData(imgDat3.data, width, height, new ImageDataSettings { colorSpace = `display-p3` })
assertEquals(defaultImageData.width, width)
assertEquals(defaultImageData.height, height)
}
}
Loading