-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas_smoke.affine
More file actions
66 lines (60 loc) · 2.57 KB
/
Copy pathcanvas_smoke.affine
File metadata and controls
66 lines (60 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: MPL-2.0
// bindings #8 — HTML5 Canvas 2D smoke test.
//
// Exercises every shipped extern via a single end-to-end drawing
// sequence: acquire context, set styles, fill rect, stroke path,
// save+translate+rotate+scale+restore the transform, draw text,
// measure text, and drawImage. The harness mocks both the canvas
// element and the 2D context.
use Deno::{Json};
use Canvas::{Ctx2D, canvasGetContext2D, canvasFillStyle, canvasStrokeStyle, canvasLineWidth, canvasGlobalAlpha, canvasFillRect, canvasStrokeRect, canvasClearRect, canvasBeginPath, canvasClosePath, canvasMoveTo, canvasLineTo, canvasArc, canvasFill, canvasStroke, canvasSave, canvasRestore, canvasTranslate, canvasRotate, canvasScale, canvasFont, canvasTextAlign, canvasTextBaseline, canvasFillText, canvasStrokeText, canvasMeasureText, canvasDrawImage, canvasDrawImageScaled};
/// Exercises the styles + rect-drawing surface.
pub fn smokeRects(canvas: Json) -> Ctx2D {
let ctx = canvasGetContext2D(canvas);
canvasFillStyle(ctx, "#ff0000");
canvasStrokeStyle(ctx, "rgba(0, 0, 0, 0.5)");
canvasLineWidth(ctx, 2.5);
canvasGlobalAlpha(ctx, 0.75);
canvasClearRect(ctx, 0.0, 0.0, 100.0, 100.0);
canvasFillRect(ctx, 10.0, 20.0, 30.0, 40.0);
canvasStrokeRect(ctx, 50.0, 60.0, 20.0, 20.0);
ctx
}
/// Exercises the path surface (begin/move/line/arc/close + fill+stroke).
pub fn smokePath(ctx: Ctx2D) -> Int {
canvasBeginPath(ctx);
canvasMoveTo(ctx, 10.0, 10.0);
canvasLineTo(ctx, 20.0, 30.0);
canvasArc(ctx, 50.0, 50.0, 10.0, 0.0, 6.283);
canvasClosePath(ctx);
canvasFill(ctx);
canvasStroke(ctx);
0
}
/// Exercises the transform stack — save / translate / rotate / scale /
/// restore — by applying a transform, drawing inside, and popping.
pub fn smokeTransform(ctx: Ctx2D) -> Int {
canvasSave(ctx);
canvasTranslate(ctx, 100.0, 100.0);
canvasRotate(ctx, 1.5708);
canvasScale(ctx, 2.0, 3.0);
canvasFillRect(ctx, 0.0, 0.0, 5.0, 5.0);
canvasRestore(ctx);
0
}
/// Exercises the text surface — font, align, baseline, fill/stroke
/// text, and measure-text round-trip (returns Json).
pub fn smokeText(ctx: Ctx2D) -> Json {
canvasFont(ctx, "16px sans-serif");
canvasTextAlign(ctx, "center");
canvasTextBaseline(ctx, "middle");
canvasFillText(ctx, "Hello", 50.0, 50.0);
canvasStrokeText(ctx, "Outlined", 50.0, 70.0);
canvasMeasureText(ctx, "Measurement")
}
/// Exercises both drawImage shapes — natural-size and scaled.
pub fn smokeImages(ctx: Ctx2D, img: Json) -> Int {
canvasDrawImage(ctx, img, 10.0, 20.0);
canvasDrawImageScaled(ctx, img, 30.0, 40.0, 100.0, 50.0);
0
}