Created
November 1, 2019 12:18
-
-
Save PlugFox/b78736096c5e5f905dd4df5f2b189b27 to your computer and use it in GitHub Desktop.
HTML canvas scroll able list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:html'; | |
const num kWidth = 480; | |
const num kHeight = 320; | |
const num kTileHeight = kHeight ~/ 10; | |
const num kListHeight = kHeight; | |
T getSelf<T>(T object) => object; | |
CanvasElement canvas, tileCanvas; | |
CanvasRenderingContext2D context, tileContext; | |
void main() { | |
canvas = CanvasElement(width: kWidth, height: kHeight); | |
context = canvas.context2D; | |
tileCanvas = CanvasElement(width: kWidth, height: kTileHeight); | |
tileContext = tileCanvas.context2D; | |
tileContext.font = "10px monospace"; | |
List<int> list = List<int>.generate(16, getSelf); | |
document.body.append(canvas); | |
num offset = 0; | |
num maxListHeight = list.length * kTileHeight; | |
drawList(list, offset, 0); | |
canvas.onWheel.listen((e) { | |
num delta = e.deltaY; | |
num tempOffset = offset + delta / 8; | |
if (!delta.isNegative && tempOffset + kListHeight >= maxListHeight) { | |
return; | |
} else if (tempOffset < 0) { | |
return; | |
} | |
int offsetIndex = offset ~/ kTileHeight; | |
offset = tempOffset; | |
drawList(list, offset, offsetIndex); | |
}); | |
} | |
void drawList(List<int> list, num offset, int offsetIndex) { | |
context.clearRect(0, 0, kWidth, kHeight); | |
for (int i = 0, l = list.length, oi; i < 11; i++) { | |
oi = offsetIndex + i; | |
if (oi < l) { | |
drawTile(list[oi], oi * kTileHeight - offset); | |
} | |
} | |
} | |
void drawTile(int value, num offset) { | |
tileContext.clearRect(0, 0, kWidth, kTileHeight); | |
tileContext.fillText(value.toRadixString(16).toUpperCase(), 10, 16); | |
context.drawImage(tileCanvas, 0, offset); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment