mirror of https://github.com/iSoron/uhabits.git
parent
7ba7edb7d4
commit
5c402b5400
@ -1 +0,0 @@
|
|||||||
org.gradle.parallel=true
|
|
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.platform.gui
|
||||||
|
|
||||||
|
class CanvasTest(val platform: Platform) {
|
||||||
|
interface Platform {
|
||||||
|
fun createCanvas(width: Int, height: Int): Canvas
|
||||||
|
fun writePng(canvas: Canvas, filename: String)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testDrawing() {
|
||||||
|
val canvas = platform.createCanvas(500, 400)
|
||||||
|
|
||||||
|
canvas.setColor(Color(0x303030))
|
||||||
|
canvas.fillRect(0.0, 0.0, 500.0, 400.0)
|
||||||
|
|
||||||
|
canvas.setColor(Color(0x606060))
|
||||||
|
canvas.setStrokeWidth(25.0)
|
||||||
|
canvas.drawRect(100.0, 100.0, 300.0, 200.0)
|
||||||
|
|
||||||
|
canvas.setColor(Color(0xFFFF00))
|
||||||
|
canvas.setStrokeWidth(1.0)
|
||||||
|
canvas.drawRect(0.0, 0.0, 100.0, 100.0)
|
||||||
|
canvas.fillCircle(50.0, 50.0, 30.0)
|
||||||
|
canvas.drawRect(0.0, 100.0, 100.0, 100.0)
|
||||||
|
canvas.fillArc(50.0, 150.0, 30.0, 90.0, 135.0)
|
||||||
|
canvas.drawRect(0.0, 200.0, 100.0, 100.0)
|
||||||
|
canvas.fillArc(50.0, 250.0, 30.0, 90.0, -135.0)
|
||||||
|
canvas.drawRect(0.0, 300.0, 100.0, 100.0)
|
||||||
|
canvas.fillArc(50.0, 350.0, 30.0, 45.0, 90.0)
|
||||||
|
|
||||||
|
canvas.setColor(Color(0xFF0000))
|
||||||
|
canvas.setStrokeWidth(2.0)
|
||||||
|
canvas.drawLine(0.0, 0.0, 500.0, 400.0)
|
||||||
|
canvas.drawLine(500.0, 0.0, 0.0, 400.0)
|
||||||
|
|
||||||
|
canvas.setFont(Font.BOLD)
|
||||||
|
canvas.setFontSize(50.0)
|
||||||
|
canvas.setColor(Color(0x00FF00))
|
||||||
|
canvas.setTextAlign(TextAlign.CENTER)
|
||||||
|
canvas.drawText("HELLO", 250.0, 100.0)
|
||||||
|
|
||||||
|
canvas.setTextAlign(TextAlign.RIGHT)
|
||||||
|
canvas.drawText("HELLO", 250.0, 150.0)
|
||||||
|
|
||||||
|
canvas.setTextAlign(TextAlign.LEFT)
|
||||||
|
canvas.drawText("HELLO", 250.0, 200.0)
|
||||||
|
|
||||||
|
canvas.setFont(Font.FONT_AWESOME)
|
||||||
|
canvas.drawText(FontAwesome.CHECK, 250.0, 300.0)
|
||||||
|
|
||||||
|
platform.writePng(canvas, "CanvasTest.png")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,110 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.platform.gui
|
||||||
|
|
||||||
|
import org.w3c.dom.*
|
||||||
|
import kotlin.browser.*
|
||||||
|
import kotlin.math.*
|
||||||
|
|
||||||
|
class HtmlCanvas(val canvas: HTMLCanvasElement) : Canvas {
|
||||||
|
|
||||||
|
val ctx = canvas.getContext("2d") as CanvasRenderingContext2D
|
||||||
|
var fontSize = 12.0
|
||||||
|
var fontWeight = ""
|
||||||
|
var fontFamily = "sans-serif"
|
||||||
|
var align = CanvasTextAlign.CENTER
|
||||||
|
|
||||||
|
override fun setColor(color: Color) {
|
||||||
|
val c = "rgb(${color.red * 255}, ${color.green * 255}, ${color.blue * 255})"
|
||||||
|
ctx.fillStyle = c;
|
||||||
|
ctx.strokeStyle = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun drawLine(x1: Double, y1: Double, x2: Double, y2: Double) {
|
||||||
|
ctx.beginPath()
|
||||||
|
ctx.moveTo(x1 + 0.5, y1 + 0.5)
|
||||||
|
ctx.lineTo(x2 + 0.5, y2 + 0.5)
|
||||||
|
ctx.stroke()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun drawText(text: String, x: Double, y: Double) {
|
||||||
|
ctx.font = "${fontWeight} ${fontSize}px ${fontFamily}"
|
||||||
|
ctx.textAlign = align
|
||||||
|
ctx.textBaseline = CanvasTextBaseline.MIDDLE
|
||||||
|
ctx.fillText(text, x, y)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun fillRect(x: Double, y: Double, width: Double, height: Double) {
|
||||||
|
ctx.fillRect(x - 0.5, y - 0.5, width + 1.0, height + 1.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun drawRect(x: Double, y: Double, width: Double, height: Double) {
|
||||||
|
ctx.strokeRect(x - 0.5, y - 0.5, width + 1.0, height + 1.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getHeight(): Double {
|
||||||
|
return canvas.height.toDouble()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getWidth(): Double {
|
||||||
|
return canvas.width.toDouble()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setFont(font: Font) {
|
||||||
|
fontWeight = if (font == Font.BOLD) "bold" else ""
|
||||||
|
fontFamily = if (font == Font.FONT_AWESOME) "FontAwesome" else "sans-serif"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setFontSize(size: Double) {
|
||||||
|
fontSize = size
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setStrokeWidth(size: Double) {
|
||||||
|
ctx.lineWidth = size
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun fillArc(centerX: Double,
|
||||||
|
centerY: Double,
|
||||||
|
radius: Double,
|
||||||
|
startAngle: Double,
|
||||||
|
swipeAngle: Double) {
|
||||||
|
val from = startAngle / 180 * PI
|
||||||
|
val to = (startAngle + swipeAngle) / 180 * PI
|
||||||
|
ctx.beginPath()
|
||||||
|
ctx.moveTo(centerX, centerY)
|
||||||
|
ctx.arc(centerX, centerY, radius, -from, -to, swipeAngle >= 0)
|
||||||
|
ctx.lineTo(centerX, centerY)
|
||||||
|
ctx.fill()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun fillCircle(centerX: Double, centerY: Double, radius: Double) {
|
||||||
|
ctx.beginPath()
|
||||||
|
ctx.arc(centerX, centerY, radius, 0.0, 2 * PI)
|
||||||
|
ctx.fill()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setTextAlign(align: TextAlign) {
|
||||||
|
this.align = when(align) {
|
||||||
|
TextAlign.LEFT -> CanvasTextAlign.LEFT
|
||||||
|
TextAlign.CENTER -> CanvasTextAlign.CENTER
|
||||||
|
TextAlign.RIGHT -> CanvasTextAlign.RIGHT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.platform.gui
|
||||||
|
|
||||||
|
import org.w3c.dom.*
|
||||||
|
|
||||||
|
class HtmlCanvasTest(val canvas: HTMLCanvasElement) : CanvasTest.Platform {
|
||||||
|
|
||||||
|
override fun createCanvas(width: Int, height: Int): Canvas {
|
||||||
|
return HtmlCanvas(canvas)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun writePng(canvas: Canvas, filename: String) {
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testDrawing() {
|
||||||
|
val test = CanvasTest(this)
|
||||||
|
test.testDrawing()
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
@ -0,0 +1,21 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Canvas Test</title>
|
||||||
|
<style>
|
||||||
|
@font-face {
|
||||||
|
font-family: "FontAwesome";
|
||||||
|
src: url(FontAwesome.ttf) format("truetype");
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<script src="../build/bundles/test.js"></script>
|
||||||
|
<canvas id="canvas" width=500 height=400></canvas>
|
||||||
|
<script>
|
||||||
|
const canvas = document.getElementById('canvas');
|
||||||
|
const test = new document.coreTest.org.isoron.platform.gui.HtmlCanvasTest(canvas);
|
||||||
|
test.testDrawing();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,15 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Mocha Tests</title>
|
||||||
|
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="mocha"></div>
|
||||||
|
<script src="../node_modules/mocha/mocha.js"></script>
|
||||||
|
<script src="../node_modules/chai/chai.js"></script>
|
||||||
|
<script>mocha.setup('bdd')</script>
|
||||||
|
<script src="../build/bundles/test.js"></script>
|
||||||
|
<script>mocha.run();</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -1,2 +1,3 @@
|
|||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var coreTest = require('core_test');
|
var coreTest = require('core_test');
|
||||||
|
document.coreTest = coreTest
|
||||||
|
Loading…
Reference in new issue