Skip to content

Commit

Permalink
Web: Add Canvas element (#1823)
Browse files Browse the repository at this point in the history
Co-authored-by: hfhbd <hfhbd@users.noreply.github.com>
  • Loading branch information
hfhbd and hfhbd committed Feb 14, 2022
1 parent 83773dc commit a528838
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
@@ -1,9 +1,11 @@
package org.jetbrains.compose.web.attributes

import org.jetbrains.compose.web.attributes.builders.saveControlledInputState
import org.jetbrains.compose.web.css.*
import org.jetbrains.compose.web.events.SyntheticSubmitEvent
import org.w3c.dom.HTMLAnchorElement
import org.w3c.dom.HTMLButtonElement
import org.w3c.dom.HTMLCanvasElement
import org.w3c.dom.HTMLFormElement
import org.w3c.dom.HTMLImageElement
import org.w3c.dom.HTMLInputElement
Expand Down Expand Up @@ -329,3 +331,9 @@ fun AttrsScope<HTMLTableCellElement>.colspan(value: Int): AttrsScope<HTMLTableCe
fun AttrsScope<HTMLTableCellElement>.rowspan(value: Int): AttrsScope<HTMLTableCellElement> =
attr("rowspan", value.toString())

/* Canvas attributes */
fun AttrsScope<HTMLCanvasElement>.width(value: CSSSizeValue<out CSSUnitLengthOrPercentage>) =
attr("width", value.toString())

fun AttrsScope<HTMLCanvasElement>.height(value: CSSSizeValue<out CSSUnitLengthOrPercentage>) =
attr("height", value.toString())
Expand Up @@ -17,6 +17,7 @@ import org.w3c.dom.HTMLAreaElement
import org.w3c.dom.HTMLAudioElement
import org.w3c.dom.HTMLBRElement
import org.w3c.dom.HTMLButtonElement
import org.w3c.dom.HTMLCanvasElement
import org.w3c.dom.HTMLDataListElement
import org.w3c.dom.HTMLDivElement
import org.w3c.dom.HTMLElement
Expand Down Expand Up @@ -93,6 +94,7 @@ private val Object: ElementBuilder<HTMLObjectElement> = ElementBuilderImplementa
private val Param: ElementBuilder<HTMLParamElement> = ElementBuilderImplementation("param")
private val Picture: ElementBuilder<HTMLPictureElement> = ElementBuilderImplementation("picture")
private val Source: ElementBuilder<HTMLSourceElement> = ElementBuilderImplementation("source")
private val Canvas: ElementBuilder<HTMLCanvasElement> = ElementBuilderImplementation("canvas")

private val Div: ElementBuilder<HTMLDivElement> = ElementBuilderImplementation("div")
private val A: ElementBuilder<HTMLAnchorElement> = ElementBuilderImplementation("a")
Expand Down Expand Up @@ -414,6 +416,18 @@ fun Source(
)
}

@Composable
fun Canvas(
attrs: AttrBuilderContext<HTMLCanvasElement>? = null,
content: ContentBuilder<HTMLCanvasElement>? = null
) {
TagElement(
elementBuilder = Canvas,
applyAttrs = attrs,
content = content
)
}

@OptIn(ComposeWebInternalApi::class)
@Composable
fun Text(value: String) {
Expand Down
11 changes: 11 additions & 0 deletions web/core/src/jsTest/kotlin/elements/AttributesTests.kt
Expand Up @@ -550,4 +550,15 @@ class AttributesTests {
check(InputMode.Search, "search")
check(InputMode.Url, "url")
}

@Test
fun canvasAttributeTest() = runTest {
composition {
Canvas({
height(400.px)
width(400.px)
})
}
assertEquals("""<canvas height="400px" width="400px"></canvas>""",root.innerHTML)
}
}
1 change: 1 addition & 0 deletions web/core/src/jsTest/kotlin/elements/ElementsTests.kt
Expand Up @@ -45,6 +45,7 @@ class ElementsTests {
Pair({ Param() }, "PARAM"),
Pair({ Picture() }, "PICTURE"),
Pair({ Source() }, "SOURCE"),
Pair({ Canvas() }, "CANVAS"),

Pair({ Div() }, "DIV"),
Pair({ A() }, "A"),
Expand Down

0 comments on commit a528838

Please sign in to comment.