【发布时间】:2016-06-08 21:45:07
【问题描述】:
我正在分析总统候选人的演讲。我有一个包含以下变量的数据文件:
> names(cl.context)
[1] "id" "category" "statement" "nchar" "polarity"
statement 变量由三个句子中的一个 category 填充。 polarity 的范围从 -1 到 1,反映句子是否具有正面偏见、中性或负面偏见。
我在 p5 中尝试做的是当用户在画布内单击鼠标时,按类别显示语句,随机 x,y 位置。语句本身根据它们的极性着色。
我终于到了开发人员控制台不会抛出任何错误并绘制画布的地步。但是当我在画布内单击时,什么也没有发生。没有语句出现。
我对 JavaScript 很陌生,因为它没有抛出错误消息,所以我无法解决问题所在。希望在这里得到一些建议。
我的 p5 代码:
var clContext;
var x;
var y;
const STATEMENTS = 118, CATEGORY = 3, QTY = STATEMENTS/CATEGORY | 0,
POLARITY = 3,
statements = Array(STATEMENTS), inds = Array(CATEGORY), polarity = Array(POLARITY);
//load the table of Clinton's words and frequencies
function preload() {
clContext = loadTable("cl_context.csv", "header");
}
function setup() {
createCanvas(647, 400);
background(51);
// Calling noStroke once here to avoid unecessary repeated function calls
noStroke();
// iterate over the table rows
for(var i=0; i<clContext.getRowCount(); i++){
//- Get data out of the relevant columns for each row -//
var inds = clContext.get(i, "category");
var statements = clContext.get(i, "statement");
var polarity = clContext.get(i, "polarity")
}
for (let i = 0; i < statements; randomCategoryStates(i++));
// create your Statement object and add to
// the statements array for use later
inds[i] = new Statement();
console.info(inds);
}
function draw() {
if(mouseClicked == true){
for(var i=0; i<inds.length; i++) {
inds[i].display();
}
}
}
function mouseClicked() {
if((mouseX < width) && (mouseY < height)) {
randomCategoryStates(~~random(CATEGORY));
redraw();
return false;
}
}
// Function to display statements by a random category with each mouse click
function randomCategoryStates(group) {
let idx = inds[group], rnd;
while ((rnd = ~~random(QTY)) == idx);
inds[group] = rnd;
}
// Function to align statements, categories, and polarity
function Statement() {
this.x = x;
this.y = y;
this.xmax = 10;
this.ymax = 4;
this.cat = inds;
this.statement = statements;
this.polarity = polarity;
// set a random x,y position for each statement
this.dx = (Math.random()*this.xmax) * (Math.random() < .5 ? -1 : 1);
this.dy = (Math.random()*this.ymax) * (Math.random() < .5 ? -1 : 1);
}
// Attach pseudo-class methods to prototype;
// Maps polarity to color and x,y to random placement on canvas
Statement.prototype.display = function() {
this.x += this.dx;
this.y += this.dy;
var cols = map(this.polarity == -1, 205, 38, 38);
var cols = map(this.polarity == 0, 148, 0, 211);
var cols = map(this.polarity == 1, 0, 145, 205);
fill(cols);
textSize(14);
text(this.statement, this.x, this.y);
};
编辑:让我感到困惑的一件事是,我在处理论坛上获得的有关此代码的帮助不包括在 draw 函数中调用 mouseClicked() 函数,所以我补充说。不完全确定我这样做是否正确,或者是否有必要。
【问题讨论】:
-
您能否提供数据文件,或者最好将您的代码缩小到minimal reproducible example?
-
你为什么要
mouseIsPressed < width?你认为这会做什么?mouseIsPressed不是布尔值,width不是数字吗? -
感谢
mouseIsPressed、@Kevin Workman 的提醒。正如我所说,我对 JavaScript 很陌生。我已更改为mouseX和mouseY。至于缩小我的代码范围,如果我这样做,我担心不会出现解决方案,因为我的代码没有在开发者控制台中抛出任何错误,所以我不确定问题出在哪里。您可以找到'cl_context.csvdata file here on GitHub. 抱歉,我最初没有包含它。
标签: javascript canvas mouseevent p5.js