Skip to content

Commit 168555c

Browse files
author
Yoshiki Shibukawa
committed
fix demo to support gopher.js
1 parent 5337dd3 commit 168555c

File tree

3 files changed

+245
-111
lines changed

3 files changed

+245
-111
lines changed

sample/demo.go renamed to sample/demo/demo.go

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,90 @@
1-
package main
1+
package demo
22

33
import (
4-
"fmt"
54
"github.com/shibukawa/nanovgo"
65
"math"
7-
8-
"log"
96
"strconv"
107
)
118

9+
const (
10+
IconSEARCH = 0x1F50D
11+
IconCIRCLEDCROSS = 0x2716
12+
IconCHEVRONRIGHT = 0xE75E
13+
IconCHECK = 0x2713
14+
IconLOGIN = 0xE740
15+
IconTRASH = 0xE729
16+
)
17+
1218
// DemoData keeps font and image handlers
1319
type DemoData struct {
14-
fontNormal, fontBold, fontIcons int
15-
images []int
20+
FontNormal, FontBold, FontIcons int
21+
Images []int
1622
}
1723

18-
func (d *DemoData) loadData(ctx *nanovgo.Context) {
19-
for i := 0; i < 12; i++ {
20-
path := fmt.Sprintf("images/image%d.jpg", i+1)
21-
d.images = append(d.images, ctx.CreateImage(path, 0))
22-
if d.images[i] == 0 {
23-
log.Fatalf("Could not load %s", path)
24-
}
25-
}
26-
27-
d.fontIcons = ctx.CreateFont("icons", "entypo.ttf")
28-
if d.fontIcons == -1 {
29-
log.Fatalln("Could not add font icons.")
30-
}
31-
d.fontNormal = ctx.CreateFont("sans", "Roboto-Regular.ttf")
32-
if d.fontNormal == -1 {
33-
log.Fatalln("Could not add font italic.")
34-
}
35-
d.fontBold = ctx.CreateFont("sans-bold", "Roboto-Bold.ttf")
36-
if d.fontBold == -1 {
37-
log.Fatalln("Could not add font bold.")
24+
func (d *DemoData) FreeData(ctx *nanovgo.Context) {
25+
for _, img := range d.Images {
26+
ctx.DeleteImage(img)
3827
}
3928
}
4029

41-
func (d *DemoData) freeData(ctx *nanovgo.Context) {
42-
for _, img := range d.images {
43-
ctx.DeleteImage(img)
30+
func RenderDemo(ctx *nanovgo.Context, mx, my, width, height, t float32, blowup bool, data *DemoData) {
31+
drawEyes(ctx, width-250, 50, 150, 100, mx, my, t)
32+
drawParagraph(ctx, width-450, 50, 150, 100, mx, my)
33+
drawGraph(ctx, 0, height/2, width, height/2, t)
34+
drawColorWheel(ctx, width-300, height-300, 250.0, 250.0, t)
35+
36+
// Line joints
37+
drawLines(ctx, 120, height-50, 600, 50, t)
38+
39+
// Line widths
40+
drawWidths(ctx, 10, 50, 30)
41+
42+
// Line caps
43+
drawCaps(ctx, 10, 300, 30)
44+
45+
drawScissor(ctx, 50, height-80, t)
46+
47+
ctx.Save()
48+
defer ctx.Restore()
49+
50+
if blowup {
51+
ctx.Rotate(sinF(t*0.3) * 5.0 / 180.0 * nanovgo.PI)
52+
ctx.Scale(2.0, 2.0)
4453
}
54+
55+
// Widgets
56+
drawWindow(ctx, "Widgets `n Stuff", 50, 50, 300, 400)
57+
var x float32 = 60.0
58+
var y float32 = 95.0
59+
drawSearchBox(ctx, "Search", x, y, 280, 25)
60+
y += 40
61+
drawDropDown(ctx, "Effects", x, y, 280, 28)
62+
popy := y + 14
63+
y += 45
64+
65+
// Form
66+
drawLabel(ctx, "Login", x, y, 280, 20)
67+
y += 25
68+
drawEditBox(ctx, "Email", x, y, 280, 28)
69+
y += 35
70+
drawEditBox(ctx, "Password", x, y, 280, 28)
71+
y += 38
72+
drawCheckBox(ctx, "Remember me", x, y, 140, 28)
73+
drawButton(ctx, IconLOGIN, "Sign in", x+138, y, 140, 28, nanovgo.RGBA(0, 96, 128, 255))
74+
y += 45
75+
76+
// Slider
77+
drawLabel(ctx, "Diameter", x, y, 280, 20)
78+
y += 25
79+
drawEditBoxNum(ctx, "123.00", "px", x+180, y, 100, 28)
80+
drawSlider(ctx, 0.4, x, y, 170, 28)
81+
y += 55
82+
83+
drawButton(ctx, IconTRASH, "Delete", x, y, 160, 28, nanovgo.RGBA(128, 16, 8, 255))
84+
drawButton(ctx, 0, "Cancel", x+170, y, 110, 28, nanovgo.RGBA(0, 0, 0, 0))
85+
86+
// Thumbnails box
87+
drawThumbnails(ctx, 365, popy-30, 160, 300, data.Images, t)
4588
}
4689

4790
func cosF(a float32) float32 {
@@ -150,7 +193,7 @@ func drawSearchBox(ctx *nanovgo.Context, text string, x, y, w, h float32) {
150193
ctx.SetFontFace("icons")
151194
ctx.SetFillColor(nanovgo.RGBA(255, 255, 255, 64))
152195
ctx.SetTextAlign(nanovgo.AlignCenter | nanovgo.AlignMiddle)
153-
ctx.Text(x+h*0.55, y+h*0.55, cpToUTF8(iconSEARCH))
196+
ctx.Text(x+h*0.55, y+h*0.55, cpToUTF8(IconSEARCH))
154197

155198
ctx.SetFontSize(20.0)
156199
ctx.SetFontFace("sans")
@@ -163,7 +206,7 @@ func drawSearchBox(ctx *nanovgo.Context, text string, x, y, w, h float32) {
163206
ctx.SetFontFace("icons")
164207
ctx.SetFillColor(nanovgo.RGBA(255, 255, 255, 32))
165208
ctx.SetTextAlign(nanovgo.AlignCenter | nanovgo.AlignMiddle)
166-
ctx.Text(x+w-h*0.55, y+h*0.55, cpToUTF8(iconCIRCLEDCROSS))
209+
ctx.Text(x+w-h*0.55, y+h*0.55, cpToUTF8(IconCIRCLEDCROSS))
167210
}
168211

169212
func drawDropDown(ctx *nanovgo.Context, text string, x, y, w, h float32) {
@@ -191,7 +234,7 @@ func drawDropDown(ctx *nanovgo.Context, text string, x, y, w, h float32) {
191234
ctx.SetFontFace("icons")
192235
ctx.SetFillColor(nanovgo.RGBA(255, 255, 255, 64))
193236
ctx.SetTextAlign(nanovgo.AlignCenter | nanovgo.AlignMiddle)
194-
ctx.Text(x+w-h*0.5, y+h*0.5, cpToUTF8(iconCHEVRONRIGHT))
237+
ctx.Text(x+w-h*0.5, y+h*0.5, cpToUTF8(IconCHEVRONRIGHT))
195238
}
196239

197240
func drawEditBoxBase(ctx *nanovgo.Context, x, y, w, h float32) {
@@ -266,7 +309,7 @@ func drawCheckBox(ctx *nanovgo.Context, text string, x, y, w, h float32) {
266309
ctx.SetFontFace("icons")
267310
ctx.SetFillColor(nanovgo.RGBA(255, 255, 255, 128))
268311
ctx.SetTextAlign(nanovgo.AlignCenter | nanovgo.AlignMiddle)
269-
ctx.Text(x+9+2, y+h*0.5, cpToUTF8(iconCHECK))
312+
ctx.Text(x+9+2, y+h*0.5, cpToUTF8(IconCHECK))
270313
}
271314

272315
func drawButton(ctx *nanovgo.Context, preicon int, text string, x, y, w, h float32, col nanovgo.Color) {

sample/sample.go

Lines changed: 32 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,97 +3,28 @@
33
package main
44

55
import (
6+
"fmt"
67
"github.com/goxjs/gl"
78
"github.com/goxjs/glfw"
89
"github.com/shibukawa/nanovgo"
910
"github.com/shibukawa/nanovgo/perfgraph"
10-
)
11-
12-
const (
13-
iconSEARCH = 0x1F50D
14-
iconCIRCLEDCROSS = 0x2716
15-
iconCHEVRONRIGHT = 0xE75E
16-
iconCHECK = 0x2713
17-
iconLOGIN = 0xE740
18-
iconTRASH = 0xE729
11+
"github.com/shibukawa/nanovgo/sample/demo"
12+
"log"
1913
)
2014

2115
var blowup bool
22-
var screenshot bool
2316
var premult bool
2417

2518
func key(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
2619
if key == glfw.KeyEscape && action == glfw.Press {
2720
w.SetShouldClose(true)
2821
} else if key == glfw.KeySpace && action == glfw.Press {
2922
blowup = !blowup
30-
} else if key == glfw.KeyS && action == glfw.Press {
31-
screenshot = true
3223
} else if key == glfw.KeyP && action == glfw.Press {
3324
premult = !premult
3425
}
3526
}
3627

37-
func renderDemo(ctx *nanovgo.Context, mx, my, width, height, t float32, data *DemoData) {
38-
drawEyes(ctx, width-250, 50, 150, 100, mx, my, t)
39-
drawParagraph(ctx, width-450, 50, 150, 100, mx, my)
40-
drawGraph(ctx, 0, height/2, width, height/2, t)
41-
drawColorWheel(ctx, width-300, height-300, 250.0, 250.0, t)
42-
43-
// Line joints
44-
drawLines(ctx, 120, height-50, 600, 50, t)
45-
46-
// Line widths
47-
drawWidths(ctx, 10, 50, 30)
48-
49-
// Line caps
50-
drawCaps(ctx, 10, 300, 30)
51-
52-
drawScissor(ctx, 50, height-80, t)
53-
54-
ctx.Save()
55-
defer ctx.Restore()
56-
57-
if blowup {
58-
ctx.Rotate(sinF(t*0.3) * 5.0 / 180.0 * nanovgo.PI)
59-
ctx.Scale(2.0, 2.0)
60-
}
61-
62-
// Widgets
63-
drawWindow(ctx, "Widgets `n Stuff", 50, 50, 300, 400)
64-
var x float32 = 60.0
65-
var y float32 = 95.0
66-
drawSearchBox(ctx, "Search", x, y, 280, 25)
67-
y += 40
68-
drawDropDown(ctx, "Effects", x, y, 280, 28)
69-
popy := y + 14
70-
y += 45
71-
72-
// Form
73-
drawLabel(ctx, "Login", x, y, 280, 20)
74-
y += 25
75-
drawEditBox(ctx, "Email", x, y, 280, 28)
76-
y += 35
77-
drawEditBox(ctx, "Password", x, y, 280, 28)
78-
y += 38
79-
drawCheckBox(ctx, "Remember me", x, y, 140, 28)
80-
drawButton(ctx, iconLOGIN, "Sign in", x+138, y, 140, 28, nanovgo.RGBA(0, 96, 128, 255))
81-
y += 45
82-
83-
// Slider
84-
drawLabel(ctx, "Diameter", x, y, 280, 20)
85-
y += 25
86-
drawEditBoxNum(ctx, "123.00", "px", x+180, y, 100, 28)
87-
drawSlider(ctx, 0.4, x, y, 170, 28)
88-
y += 55
89-
90-
drawButton(ctx, iconTRASH, "Delete", x, y, 160, 28, nanovgo.RGBA(128, 16, 8, 255))
91-
drawButton(ctx, 0, "Cancel", x+170, y, 110, 28, nanovgo.RGBA(0, 0, 0, 0))
92-
93-
// Thumbnails box
94-
drawThumbnails(ctx, 365, popy-30, 160, 300, data.images, t)
95-
}
96-
9728
func main() {
9829
err := glfw.Init(gl.ContextWatcher)
9930
if err != nil {
@@ -111,16 +42,14 @@ func main() {
11142
window.SetKeyCallback(key)
11243
window.MakeContextCurrent()
11344

114-
//ctx, err := nanovgo.NewContext( nanovgo.ANTIALIAS | nanovgo.STENCIL_STROKES | nanovgo.DEBUG)
115-
ctx, err := nanovgo.NewContext(nanovgo.StencilStrokes | nanovgo.Debug)
45+
ctx, err := nanovgo.NewContext(nanovgo.AntiAlias | nanovgo.StencilStrokes /* | nanovgo.Debug*/)
11646
defer ctx.Delete()
11747

11848
if err != nil {
11949
panic(err)
12050
}
12151

122-
demoData := &DemoData{}
123-
demoData.loadData(ctx)
52+
demoData := LoadDemo(ctx)
12453

12554
glfw.SwapInterval(0)
12655

@@ -150,7 +79,7 @@ func main() {
15079

15180
ctx.BeginFrame(winWidth, winHeight, pixelRatio)
15281

153-
renderDemo(ctx, float32(mx), float32(my), float32(winWidth), float32(winHeight), t, demoData)
82+
demo.RenderDemo(ctx, float32(mx), float32(my), float32(winWidth), float32(winHeight), t, blowup, demoData)
15483
fps.RenderGraph(ctx, 5, 5)
15584

15685
ctx.EndFrame()
@@ -160,5 +89,30 @@ func main() {
16089
glfw.PollEvents()
16190
}
16291

163-
demoData.freeData(ctx)
92+
demoData.FreeData(ctx)
93+
}
94+
95+
func LoadDemo(ctx *nanovgo.Context) *demo.DemoData {
96+
d := &demo.DemoData{}
97+
for i := 0; i < 12; i++ {
98+
path := fmt.Sprintf("images/image%d.jpg", i+1)
99+
d.Images = append(d.Images, ctx.CreateImage(path, 0))
100+
if d.Images[i] == 0 {
101+
log.Fatalf("Could not load %s", path)
102+
}
103+
}
104+
105+
d.FontIcons = ctx.CreateFont("icons", "entypo.ttf")
106+
if d.FontIcons == -1 {
107+
log.Fatalln("Could not add font icons.")
108+
}
109+
d.FontNormal = ctx.CreateFont("sans", "Roboto-Regular.ttf")
110+
if d.FontNormal == -1 {
111+
log.Fatalln("Could not add font italic.")
112+
}
113+
d.FontBold = ctx.CreateFont("sans-bold", "Roboto-Bold.ttf")
114+
if d.FontBold == -1 {
115+
log.Fatalln("Could not add font bold.")
116+
}
117+
return d
164118
}

0 commit comments

Comments
 (0)