Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b2745bb

Browse files
authoredMay 10, 2024··
Remove d3 dependency from RL env rendering process (#844)
1 parent 22cafd4 commit b2745bb

File tree

4 files changed

+58
-51
lines changed

4 files changed

+58
-51
lines changed
 

‎js/renderer/rl/gomoku.js

+17-18
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,24 @@ class ManualPlayer {
137137
action(board) {
138138
const width = this._renderer._size[0]
139139
const height = this._renderer._size[1]
140-
this._obj = document.createElementNS('http://www.w3.org/2000/svg', 'g')
141-
this._renderer.svg.appendChild(this._obj)
142-
const check = document.createElementNS('http://www.w3.org/2000/svg', 'rect')
143-
check.setAttribute('x', 0)
144-
check.setAttribute('y', 0)
145-
check.setAttribute('width', width)
146-
check.setAttribute('height', height)
147-
check.setAttribute('opacity', 0)
148-
this._obj.appendChild(check)
140+
this._obj = this._renderer.svg.appendChild(document.createElementNS('http://www.w3.org/2000/svg', 'g'))
141+
const dw = width / board.size[1]
142+
const dh = height / board.size[0]
143+
149144
return new Promise(resolve => {
150-
check.onclick = e => {
151-
const pos = d3.pointer(e)
152-
const cell = [
153-
Math.floor((pos[1] / width) * board.size[0]),
154-
Math.floor((pos[0] / height) * board.size[1]),
155-
]
156-
this._obj.remove()
157-
this._obj = null
158-
resolve(cell)
145+
for (let i = 0; i < board.size[0]; i++) {
146+
for (let j = 0; j < board.size[1]; j++) {
147+
const rect = this._obj.appendChild(document.createElementNS('http://www.w3.org/2000/svg', 'rect'))
148+
rect.setAttribute('x', j * dw)
149+
rect.setAttribute('y', i * dh)
150+
rect.setAttribute('width', dw)
151+
rect.setAttribute('height', dh)
152+
rect.setAttribute('opacity', 0)
153+
rect.onclick = () => {
154+
this.close()
155+
resolve([i, j])
156+
}
157+
}
159158
}
160159
})
161160
}

‎js/renderer/rl/grid.js

+23-14
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,31 @@ export default class GridMazeRenderer {
5353
init(r) {
5454
const width = 960
5555
const height = 500
56-
const base = document.createElement('div')
57-
base.onclick = e => {
58-
const p = d3.pointer(e)
59-
const idx = this.renderer.env._size[0] / width
60-
const idy = this.renderer.env._size[1] / height
61-
const x = Math.floor(p[0] * idx)
62-
const y = Math.floor(p[1] * idy)
63-
this.renderer.env._points.push([x, y])
64-
e.stopPropagation()
65-
setTimeout(() => {
66-
this.renderer.render()
67-
}, 0)
68-
}
69-
r.appendChild(base)
56+
const base = r.appendChild(document.createElement('div'))
57+
base.style.position = 'relative'
7058
this._envrenderer = new Renderer(this.renderer.env, { width, height, g: base })
7159
this._envrenderer.init()
60+
61+
const clicker = base.appendChild(document.createElement('div'))
62+
const dx = width / this.renderer.env._size[0]
63+
const dy = height / this.renderer.env._size[1]
64+
for (let i = 0; i < this.renderer.env._size[0]; i++) {
65+
for (let j = 0; j < this.renderer.env._size[1]; j++) {
66+
const ccell = clicker.appendChild(document.createElement('div'))
67+
ccell.style.position = 'absolute'
68+
ccell.style.top = `${dy * j}px`
69+
ccell.style.left = `${dx * i}px`
70+
ccell.style.width = `${dx}px`
71+
ccell.style.height = `${dy}px`
72+
ccell.onclick = e => {
73+
this.renderer.env._points.push([i, j])
74+
e.stopPropagation()
75+
setTimeout(() => {
76+
this.renderer.render()
77+
}, 0)
78+
}
79+
}
80+
}
7281
}
7382

7483
render(best_action) {

‎js/renderer/rl/maze.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class SmoothMazeRenderer {
1515
rect.style.width = `${this._width}px`
1616
rect.style.height = `${this._height}px`
1717
rect.onclick = e => {
18-
const p = d3.pointer(e)
18+
const p = [e.offsetX, e.offsetY]
1919
const dx = this._width / this.renderer.env._map_resolution[0]
2020
const dy = this._height / this.renderer.env._map_resolution[1]
2121
const x = Math.floor(p[0] / dx)

‎js/renderer/rl/reversi.js

+17-18
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,24 @@ class ManualPlayer {
133133
action(board) {
134134
const width = this._renderer._size[0]
135135
const height = this._renderer._size[1]
136-
this._obj = document.createElementNS('http://www.w3.org/2000/svg', 'g')
137-
this._renderer.svg.appendChild(this._obj)
138-
const check = document.createElementNS('http://www.w3.org/2000/svg', 'rect')
139-
check.setAttribute('x', 0)
140-
check.setAttribute('y', 0)
141-
check.setAttribute('width', width)
142-
check.setAttribute('height', height)
143-
check.setAttribute('opacity', 0)
144-
this._obj.appendChild(check)
136+
this._obj = this._renderer.svg.appendChild(document.createElementNS('http://www.w3.org/2000/svg', 'g'))
137+
const dw = width / board.size[1]
138+
const dh = height / board.size[0]
139+
145140
return new Promise(resolve => {
146-
check.onclick = e => {
147-
const pos = d3.pointer(e)
148-
const cell = [
149-
Math.floor((pos[1] / width) * board.size[0]),
150-
Math.floor((pos[0] / height) * board.size[1]),
151-
]
152-
this._obj.remove()
153-
this._obj = null
154-
resolve(cell)
141+
for (let i = 0; i < board.size[0]; i++) {
142+
for (let j = 0; j < board.size[1]; j++) {
143+
const rect = this._obj.appendChild(document.createElementNS('http://www.w3.org/2000/svg', 'rect'))
144+
rect.setAttribute('x', j * dw)
145+
rect.setAttribute('y', i * dh)
146+
rect.setAttribute('width', dw)
147+
rect.setAttribute('height', dh)
148+
rect.setAttribute('opacity', 0)
149+
rect.onclick = () => {
150+
this.close()
151+
resolve([i, j])
152+
}
153+
}
155154
}
156155
})
157156
}

0 commit comments

Comments
 (0)
Please sign in to comment.