コード例 #1
0
ファイル: app.go プロジェクト: kirillrdy/gopherjs
func mainPage(document dom.Document) {

	js.SetTitle("Main Page")
	js.SetBody(html.Div().Children(
		html.H1().Id(h1Id).Text("Click Me"),
		html.A().Href(paths.second).Text("Second page"),
	))

	h1 := document.QuerySelector(h1Id.Selector())

	h1.AddEventListener("click", true, func(event dom.Event) {
		js.NavigateTo(paths.second)
	})
}
コード例 #2
0
func snakeMove(quit chan struct{}, d dom.Document) {
	// move snake: get next position
	npos := nextPosition(goDir, snakeBody[0])

	// check if snake eat itself
	for _, pos := range snakeBody {
		if pos.row == npos.row && pos.col == npos.col {
			close(quit)
			d.GetElementByID("info").SetTextContent("Game Over!")
			return
		}
	}

	// add next position to snake body
	// FIXME: how to prepend npos to snakeBody efficiently???
	var hpos []position
	hpos = append(hpos, npos)
	for idx, _ := range snakeBody {
		hpos = append(hpos, snakeBody[idx])
	}
	snakeBody = hpos
	sel := fmt.Sprintf("div[data-row=\"%d\"][data-col=\"%d\"]",
		npos.row, npos.col)
	d.QuerySelector(sel).Class().Add("select")
	if foodPosition.row == npos.row && foodPosition.col == npos.col {
		// snake eat food
		d.QuerySelector(sel).Class().Remove("food")
		// generate food
		foodPosition = randomFood(snakeBody)
		sel = fmt.Sprintf("div[data-row=\"%d\"][data-col=\"%d\"]",
			foodPosition.row, foodPosition.col)
		d.QuerySelector(sel).Class().Add("food")
	} else {
		// snake does not eat food
		// move snake: remove last position
		pos := snakeBody[len(snakeBody)-1]
		snakeBody = snakeBody[:len(snakeBody)-1]
		sel = fmt.Sprintf("div[data-row=\"%d\"][data-col=\"%d\"]",
			pos.row, pos.col)
		d.QuerySelector(sel).Class().Remove("select")
	}
}