【发布时间】:2016-06-11 11:57:58
【问题描述】:
我有一个 .csv 文件,我通过 p5.js 草图在 JavaScript 中调用它。其中一个字段包含范围从 103 字符到 328 字符的句子。我的脚本调用数据并随机显示在画布上。因为有些句子很长,在画布上不合适,所以我想把它们分成 2 行或 3 行的字符串。
我已经阅读了 JavaScript 文档中的 Template Literals 和 RegExp,但所有示例都使用写成变量的字符串变量。因此,例如,在我的数据中是这样的:
var myString = `We can lift up people and places who've been left out,
from our inner cities to Appalachia,
in every manufacturing town hollowed out when the factory closed,
every community scarred by substance abuse,
every home where a child goes to bed hungry.`
Template Literal 将作为多行对象打印到画布上。但我需要做的是让 JavaScript 从我的数据中的 statements 数组创建一个多行对象。
我有一个constructor 和一个prototype 来格式化句子的颜色、大小、x/y 位置和运动。
// Function to align statements, categories, and polarity
function Statement(category, polarity, statement) {
this.category = category;
this.statement = statement;
this.polarity = polarity;
this.x = random(width/2);
this.y = random(height);
this.dx = random(-speed, speed);
this.dy = random(-speed, speed);
}
// 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;
if(this.x > width+10){
this.x = -10
}
if(this.y > height+10) {
this.y = -10
}
if(this.polarity == -1){
fill(205, 38, 38);
}
else if(this.polarity == 1){
fill(0, 145, 205);
}
else{
fill(148, 0, 211);
}
textSize(14);
text(this.statement, this.x, this.y);
}
所以我想我想知道我是否需要创建一个RegExp,例如String.split("[\\r\\n]+") 并将\r\n 添加到数据中,如果需要,我会将它放在我的脚本中的什么位置。我在Statement.display.prototype 中尝试过,但它似乎破坏了整个脚本,因为语句无法加载。
编辑:我有些不安地添加了这个编辑,因为我因为没有生成 Minimal, Complete, Verifiable 示例而被钉牢,而“最小”是我被钉牢的部分。也就是说,这是我的代码的顶部。
var clContext;
var speed = 0.8;
var statements = [];
var category = [];
var canvas;
//load the table of Clinton's words and frequencies
function preload() {
clContext = loadTable("cl_context_rev.csv", "header");
}
function setup() {
canvas = createCanvas(680, 420);
canvas.mousePressed(inWidth);
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++) {
var category = clContext.get(i, "category");
var statement = clContext.get(i, "statement");
var polarity = clContext.get(i, "polarity");
statements[i] = new Statement(category, polarity, statement);
}
}
function draw() {
if (mouseIsPressed) {
background(51);
for (var i = 0; i < statements.length; i++) {
statements[i].display();
}
}
}
我添加它只是为了为我尝试拆分的数据类型提供上下文。似乎有两点我可以进行拆分:在设置中创建的statement 数组,或者来自构造函数的statements 数组。这意味着如果我进入我的数据文件并在我想要拆分的位置添加\n,这很容易,因为只有 20 条语句,那么最好如何以及在哪里构造一个可以拆分这些行的RegExp?
【问题讨论】:
-
您可以通过定义最大长度,然后通过在长度限制内的最后一个单词的末尾将其拆分为数组项来减少字符串。我在这个答案中做了类似的工作stackoverflow.com/questions/37361878/…
-
谢谢@Redu,但不幸的是,他也没有这样做。我尝试使用我的 var 名称对其进行调整,每次尝试使用各种配置运行它时,我都会收到
statement.split is not a function错误。我不确定,但这可能是 p5.js 的问题。我曾尝试过其他split方法,我遇到过并且总是遇到该错误。我将尝试在 p5.js 的 GitHub 存储库中创建一个issue,但它们并不是最快的响应者。 -
statement.split is not a function是一个 TypeError 并且意味着语句根本不是字符串类型。你检查过typeof statement吗? -
是的,@Redu,我查过了,它是字符串类型的。
-
这很不寻常。当您在应用程序中喜欢
console.log(String.prototype.split())时,您是否看到function split() { [native code] }..?
标签: javascript regex string p5.js template-literals