我正在使用的代码应该使图像square.png移动到光标。但是,代码无法运行。
我试图在代码中查找拼写错误和错误。
package main
import "fmt"
func main() {
// Arguments
arr := []string{"foo", "bar", "baz", "bee", "feo", "boo", "bak"}
combinations := make([][]string, 0)
k := 4
n := len(arr)
// Execution starts from here
if k > n {
panic("invalid requirement")
}
pos := make([]int, k) // this variable is used to plot the unique combination of elements
// initialize an array with first ever plotting possitions
i := 0
c := k
for c > 0 {
c--
pos[i] = c
i++
}
combinations = append(combinations, getCombination(arr, pos, k))
// Let's begin the work
x := 0
ctr := 1 // counter is use to calculate total iterations
for pos[x] < n-(x+1) {
ctr++
pos[x]++
combinations = append(combinations, getCombination(arr, pos, k))
if pos[x] == n-(x+1) && x+1 < k {
x++
i := x
s := pos[x] + 1
for i > 0 {
i--
s++
pos[i] = s
}
// continue to next index
continue
}
x = 0
}
fmt.Println("total # iterations: --> ", ctr)
fmt.Println(combinations, "\ntotal # combinations: ", len(combinations))
}
func getCombination(arr []string, pos []int, k int) []string {
combination := make([]string, k)
for i, j := k-1, 0; i >= 0; i, j = i-1, j+1 {
combination[j] = arr[pos[i]]
}
return combination
}
这是来自html文档中的脚本标签之间。
应该在鼠标指针所在的位置绘制正方形,但是根据Chrome开发工具,变量“ X”和“相似Y”在“加载”函数中无法识别。
答案 0 :(得分:0)
正如@eepascarello所说,您正在尝试访问超出范围的变量。您可以将similarX
和similarY
变量移出mousemove事件的范围之外
var canvas = document.createElement('canvas');
canvas.width = 800;
canvas.height = 600;
document.body.appendChild(canvas);
//possible you want to set these to something besides 0
var similarX = 0;
var similarY = 0;
document.addEventListener('mousemove',function(event){
similarX = event.clientX;
similarY = event.clientY;
document.getElementById('body').innerHTML = "x:" + similarX + ", y:" + similarY;
})
var square = new Image();
square.src = 'square.png';
window.addEventListener('load' , start);
var c = canvas.getContext('2d');
function start() {
c.fillStyle = 'green';
c.fillRect = 10, 10, 30, 30;
c.drawImage(square,similarX , similarY);
window.requestAnimationFrame(start)
}
document.body.appendChild(canvas);
请注意,这不会为您提供在mousemove
上重新定位矩形的功能,但是它将使您能够无错误地进行加载。要更新mousemove
,您可能需要做一些事情,例如在start()
事件结束时致电mousemove
;