-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathpath.go
More file actions
71 lines (63 loc) · 1.87 KB
/
Copy pathpath.go
File metadata and controls
71 lines (63 loc) · 1.87 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
67
68
69
70
71
// Copyright (c) 2025, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This is adapted from https://github.com/tdewolff/canvas
// Copyright (c) 2015 Taco de Wolff, under an MIT License.
//go:build js
package htmlcanvas
import (
"cogentcore.org/core/math32"
"cogentcore.org/core/paint/ppath"
"cogentcore.org/core/paint/render"
)
func (rs *Renderer) writePath(pt ppath.Path) {
rs.ctx.Call("beginPath")
for scanner := pt.Scanner(); scanner.Scan(); {
end := scanner.End()
switch scanner.Cmd() {
case ppath.MoveTo:
rs.ctx.Call("moveTo", end.X, end.Y)
case ppath.LineTo:
rs.ctx.Call("lineTo", end.X, end.Y)
case ppath.QuadTo:
cp := scanner.CP1()
rs.ctx.Call("quadraticCurveTo", cp.X, cp.Y, end.X, end.Y)
case ppath.CubeTo:
cp1, cp2 := scanner.CP1(), scanner.CP2()
rs.ctx.Call("bezierCurveTo", cp1.X, cp1.Y, cp2.X, cp2.Y, end.X, end.Y)
case ppath.Close:
rs.ctx.Call("closePath")
}
}
}
func (rs *Renderer) RenderPath(pt *render.Path) {
if pt.Path.Empty() {
return
}
style := &pt.Context.Style
p := pt.Path
if !ppath.ArcToCubeImmediate {
p = p.ReplaceArcs() // TODO: should we do this in writePath?
}
rs.setTransform(&pt.Context)
if style.HasFill() || style.HasStroke() {
rs.writePath(pt.Path)
}
rs.curRect = pt.Path.FastBounds().ToRect() // TODO: more performance optimized approach (such as only computing for gradients)?
if style.HasFill() {
rs.setFill(style.Fill.Color)
rs.ctx.Call("fill", style.Fill.Rule.String())
}
if style.HasStroke() {
scale := math32.Sqrt(math32.Abs(pt.Context.Cumulative.Det()))
if scale != 1 {
// note: this is a hack to get the effect of [ppath.VectorEffectNonScalingStroke]
stk := style.Stroke
stk.Width.Dots /= scale
rs.setStroke(&stk)
} else {
rs.setStroke(&style.Stroke)
}
rs.ctx.Call("stroke")
}
}