- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请原谅格式设置——在这个问题中它一直给我“你的代码未格式化”错误。我有一个由打印图像的 Canvas 组成的代码。然后,我跟踪用户的鼠标坐标,如果它们与文本文件中找到的坐标匹配,我想提示用户。这是一个网络应用程序,用于补充 C++ OpenCV 程序,用于检测 Blob 及其存在的坐标。文本文件采用以下格式:label x y
。
001 101 305
是 blob 001 中坐标 (101, 305) 的行。PHP 正在读取每一行并在空格处爆炸。
文本文件如下所示:
001 101 303
001 101 304
001 101 305
001 101 306
001 101 307
001 101 308
001 101 309
001 101 310
001 102 301
001 102 302
<script> // functions to make canvas and place PNG overlay image
<?php
$XYFile = fopen("cpp/textdump/coordinates/imagetesting.txt.txt","r") or die ("file reading error");
$coordinates = array(); // create coordinates array
while (!feof($XYFile)) { // while not at last line of file
$uncatXY = fgets($XYFile); // read one line of file
$splitXY = explode(" ", $uncatXY); // create new array element when a space is present
$label = $splitXY[0]; // declare blob label
$x = $splitXY[1]; // declare x
$y = $splitXY[2]; // declare y
array_push($coordinates, $label, $x, $y);
} // push into coordinates array
fclose($XYFile); // close file
?>
var js_array = <?php echo(json_encode($coordinates)); ?> // convert PHP array to javascript object notation format
// requires PHP 5.2 or higher, which I believe to be on the server. It's certainly on my localhost server.
console.log(js_array); // dev
$(document).ready(function() {
window.onload = function() {
var c = document.getElementById("solarCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "#00FFFF"; // cyan fill
var img = document.getElementById("testimage");
img.src = 'cpp/images/PNG/imagetesting.png'; //"cpp/images/" + date + ".png";
ctx.drawImage(img, 0, 0);
c.addEventListener('click', function() { }, false);
// functions for color & coordinates on mouse movement/clicl
$('#solarCanvas').mousemove(function(e) {
var c = document.getElementById("solarCanvas");
var ctx = c.getContext("2d"); // temp declaration bug fix -- can't find root cause, so redec
var pos = findPos(this);
var x = e.pageX - pos.x;
var y = e.pageY - pos.y;
var coord = "x=" + x + ", y=" +y;
var p = ctx.getImageData(x, y, 1, 1).data;
var hex = ("#" + 000000 + rgbToHex(p[0], p[1], p[2]).slice(-6));
$('#status').html(coord + "<br>" + hex);
console.log(x + "," + y + "---" + coord + " at " + hex);
});
$('#solarCanvas').click(function(e) {
var c = document.getElementById("solarCanvas");
var ctx = c.getContext("2d");
var pos = findPos(this);
var xNum = e.pageX - pos.x;
var yNum = e.pageY - pos.y;
var xStr = xNum.toString();
var yStr = yNum.toString();
for(var i = 0; i < js_array.length; i++) {
if(js_array[i] === xStr) {
return i;
console.log(i);
if(i > -1) {
alert("yahoo!");
}
}
}
console.log(js_array);
console.log(xStr);
console.log(yStr);
});
}
});
// old architecutre
// mixed array_search ( mixed $needle, array $haystack [, bool $strict = false ] );
// searches haystack for needle
// pseudocode
//if ((e.pageX - pos.x) exists in col 2 && (e.pageY-pos.y) is in same row in col 3) {
</script>
我已经使用过该代码的许多版本,并且正在发布我现在拥有的内容。我目前正在严格使用 X 坐标。一旦它起作用,我将添加 Y。[...]我考虑到 PHP Explode & json_encode 正在保存字符串这一事实,所以我做了一个 .toString() 函数,但这似乎根本没有帮助。例如,我的控制台日志类似于 x/y/数组中 x 的位置/数组中 y 的位置
101
305
-1
-1
最佳答案
这里稍微重写了您的代码。
我做了什么:
PHP 代码:
<?php
$file = file("cpp/textdump/coordinates/imagetesting.txt.txt"");
$coordinates = array();
for ($line in $file) {
array_push($coordinates, explode(" ", $line);
}
?>
var coords = <?= json_encode($coordinates) ?>;
JS:
$('#solarCanvas').click(function(e) {
var c = document.getElementById("solarCanvas");
var ctx = c.getContext("2d");
var pos = findPos(this);
var xNum = e.pageX - pos.x;
var yNum = e.pageY - pos.y;
var xStr = xNum.toString();
var yStr = yNum.toString();
for(var i = 0; i < coords.length; i++) {
if(coords[i][1] === xStr && coords[i][2] === yStr) {
return i;
console.log('coord found', coords[i]);
}
}
console.log(coords);
console.log(xStr);
console.log(yStr);
});
关于javascript - 扫描多维json编码数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31709536/
我的数据库表是: author book repeat ------ ----
众所周知,我们可以简化 SELECT * FROM A WHERE X=1 OR X=4 OR X=9 如下: SELECT * FROM A WHERE X IN (1,4,9) “多维”条件是否有
我在大学时正在编写 Android 应用程序。 用户被询问必须回答的问题。 我通过服务器的 HTTP 请求/响应获取问题。它们以字符串形式传递。该字符串看起来像这样,有两个问题: (requested
我怎样才能在 C++ 中使用 vector 制作这样的表格: 65 A 66 B 67 C 我是用这样的动态二维数组完成的: int** ary = new int*[2]; for (int
运行时: if (data.custaccount.webaddress) { alert('found it'); } 我得到了错误 data.custaccount is undefined
如何按名称对对象数组进行排序。 数组构造数组的例子: object[] o = new object[items.Count+(row-exists)]; int i = 0;
如何创建二维 vector ?我知道在二维数组中,我可以这样表达: a[0][1]=98; a[0][2]=95; a[0][3]=99; a[0][4]=910; a[1][0]=98; a[1][
这是我的第一个问题。 我有很多组数据。它们中的每一个都应该在 DataFrame 中呈现。我试图通过将 DataFrame 作为多维元组的一项来实现这一点,例如: data[0][1].Glucose
有人可以建议改进我对多维 lstm 的实现吗? 它非常慢并且使用大量内存。 class MultiDimentionalLSTMCell(tf.nn.rnn_cell.RNNCell): """ Ad
我已为我在另一个基于 WPF 的应用程序中使用的 Azure 移动服务添加了到 MVC 站点的连接。 我不太熟悉如何通过移动服务在 MVC 中显示数据,找不到任何很好的示例来展示如何在页面上(例如在网
这样写对吗?有没有更好的写法呢?这与我正在使用的真实数据类似,我想确保将对象或数组正确嵌套在 JSON 文件中。 var data = [ { "department": "I
我有一个HashMap当我调用 .toString() 时,它看起来像这样: {somekey=false, anotherKey=someString, thirdKey={nestedKey=he
是否有更 pythonic 的方式来执行以下操作: import numpy as np def diagonal(A): (x,y,y) = A.shape diags = []
我有以下结构: import java.util.LinkedHashMap; ... LinkedHashMap level0 = new LinkedHashMap(); LinkedHashMa
如何访问 Arraylist 中的整数数组(两者均可调整大小)? 到目前为止我有这个代码: List vertices_passed = new ArrayList(); 我想进入vertices_p
我想知道 MiniZinc 语言中是否可以有(多维)数组的数组。 确实,我想解决 worker 的时间表问题。我的目标是检查它们是否每周至少有 1 天可用。每个 worker 都由一个整数索引,我每周
这个问题已经有答案了: how is axis indexed in numpy's array? (5 个回答) 已关闭 3 年前。 我还没有理解 NumPy 中多维数组中的轴之间的区别。你能给我解
我试图 $.post 包含一些表单数据以及数组中的一些其他数据。我可以将数组插入表单数据的其余部分,但我丢失了数组中的键,并且数据仅表示为字符串,我想我要求的是多维数组? 这是当前的响应字符串: ar
我是 numpy 新手,试图理解 here 中的以下示例。我无法理解的输出 >>> palette[image] 当索引数组 a 是多维时,单个索引数组引用 a 的第一个维度。以下示例通过使用调色板将
这个问题已经有答案了: JavaScript associative array to JSON (5 个回答) JSON.stringify doesn't work with normal Jav
我是一名优秀的程序员,十分优秀!