Пример #1
0
func registerClock() {
	c := ko.NewComponent("clock")
	c.Template.Markup = "<canvas></canvas>"
	c.ViewModel.Creator = func(params *js.Object, info *ko.ComponentInfo) ko.ViewModel {
		c := canvas.New(info.Element.QuerySelector("canvas").Object)
		c.Width = 200
		c.Height = 200
		if params.Get("width") != js.Undefined {
			c.Width = params.Get("width").Int()
		}
		if params.Get("height") != js.Undefined {
			c.Height = params.Get("height").Int()
		}
		if c.Width > c.Height {
			c.Width = c.Height
		} else {
			c.Height = c.Width
		}
		clock := newSimClock(c.GetContext2D(), c.Width, c.Height)
		clock.draw(time.Now())
		go func() {
			for t := range time.Tick(time.Millisecond * 100) {
				clock.draw(t)
			}
		}()
		return nil
	}
	ko.RegisterComponent(c)
}
Пример #2
0
func main() {
	dom.OnDOMContentLoaded(func() {
		s := dom.GetElementById("gopherjs")
		style := dom.GetComputedStyle(s)

		el := canvas.New(dom.CreateElement("canvas").Object)
		cw, _ = strconv.ParseFloat(style.GetPropertyValue("width")[:3], 64)
		ch, _ = strconv.ParseFloat(style.GetPropertyValue("height")[:3], 64)
		el.Width = int(cw)
		el.Height = int(ch)
		el.AddEventListener(dom.EvtMousemove, func(e *dom.Event) {
			e.PreventDefault()
			x := float64(e.LayerX)
			y := float64(e.LayerY)
			makeParticles(x, y, 5)
		})
		ctx = el.GetContext2D()
		ctx.GlobalCompositeOperation = canvas.CompositeLighter
		s.AppendChild(el.Element)
		raf.RequestAnimationFrame(run)
	})
}
Пример #3
0
func main() {
	dom.OnDOMContentLoaded(func() {
		s := dom.Body()
		// set full window and black background
		s.Style.SetProperty("margin", "0")
		s.Style.SetProperty("background", "#222")

		el := canvas.New(dom.CreateElement("canvas").Object)
		cw = float64(dom.Window().InnerWidth)
		ch = float64(dom.Window().InnerHeight)
		el.Width = int(cw)
		el.Height = int(ch)
		el.AddEventListener(dom.EvtMousemove, func(e *dom.Event) {
			e.PreventDefault()
			x := float64(e.ClientX)
			y := float64(e.ClientY)
			makeParticles(x, y, 5)
		})
		ctx = el.GetContext2D()
		ctx.GlobalCompositeOperation = canvas.CompositeLighter
		dom.Body().AppendChild(el.Element)
		raf.RequestAnimationFrame(run)
	})
}