PS3 MDDN 342 2016
An example of how to get started with the Online Chatbot.
Do not modify the skech.js: all changes should be made to bot.js.
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.3/p5.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.3/addons/p5.dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/seedrandom/2.4.0/seedrandom.min.js"></script>
<script src="https://d3js.org/d3-random.v1.min.js"></script>
<script language="javascript" type="text/javascript" src="focusedRandom.js"></script>
<script language="javascript" type="text/javascript" src=".purview_helper.js"></script>
<script src="tracery.js"></script>
<script language="javascript" type="text/javascript" src="bot.js"></script>
<script language="javascript" type="text/javascript" src="sketch.js"></script>
<style>
body {padding: 0; margin: 0;}
ul li {
list-style:none;
overflow:hidden;
border:1px solid #dedede;
margin:5px;
padding:5px;
}
.media img {
max-width:440px;
max-height:220px;
}
</style>
</head>
<body style="background-color:white">
<div id="canvasContainer"></div>
<pre>
<p id="tweet_text">
</p>
</pre>
<hr>
<div id="tweetExamples"></div>
</body>
// images for the background
var closed_images = [
"sunny.jpg",
"cloudy.jpg",
"windy.jpg",
"rainy.jpg"
]
// 10 day high temperature forecast
var closed_data = [
[20, 22, 24, 22, 24, 22, 20, 15, 10, 15],
[30, 29, 27, 25, 25, 23, 22, 22, 23, 20],
[10, 12, 10, 12, 10, 12, 10, 12, 10, 12]
]
function bot() {
// make this true once image has been drawn
this.have_drawn = false;
// helper function to draw an owl
this.owl = function(x, y, g, s) {
push();
translate(x, y);
scale(s); // Set the createCanvas
stroke(g); // Set the gray value
strokeWeight(70);
line(0, -35, 0, -65); // Body
noStroke();
fill(255-g);
ellipse(-17.5, -65, 35, 35); // Left eye dome
ellipse(17.5, -65, 35, 35); // Right eye dome
arc(0, -65, 70, 70, 0, PI); // Chin
fill(g);
ellipse(-14, -65, 8, 8); // Left eye
ellipse(14, -65, 8, 8); // Right eye
quad(0, -58, 4, -51, 0, -44, -4, -51); // Beak
pop();
}
// return true if image has been drawn
this.isDone = function() {
return this.have_drawn;
}
// load all external images or data
this.preload = function() {
// chose a random image from the list
var image_name = random(closed_images);
println(image_name);
// load the image the variable img
this.img = loadImage(image_name)
}
this.setup = function() {
// chose a random set of data from the list of data
this.temperatures = random(closed_data);
println(this.temperatures);
}
this.grammar = {
"animal": ["bird","animal","creature","owl","night-watcher"],
"reaction": ["vexing","perplexing","exciting","wistful","enigmatic"],
"prediction": ["prediction", "forecast", "prognostication"],
"timeunit": ["day", "night"]
}
this.respond = function() {
// draw the background image
image(this.img);
// chose how many owls to draw
var num_owls = Math.floor(focusedRandom(5, 11, 3, 7));
// draw the owls based on the data in temperatures
var spacing = 400 / num_owls;
for (i=0; i<num_owls; i++) {
var xpos = 35 + spacing * i;
var gray = int(focusedRandom(0, 102, 3))
var scalar = focusedRandom(0.15, 0.75, 2);
this.owl(xpos, 220-2*this.temperatures[i], gray, scalar);
}
// set have_drawn to true since we have completed
this.have_drawn = true;
// construct the message
var grammar = tracery.createGrammar(this.grammar);
var seed = "" + num_owls + " #timeunit# #reaction# #animal.s# #prediction#";
var message = grammar.flatten(seed);
return message;
}
}
{
}
function resetFocusedRandom() {
return Math.seedrandom(arguments);
}
function focusedRandom(min, max, focus, mean) {
// console.log("hello")
if(max === undefined) {
max = min;
min = 0;
}
if(focus === undefined) {
focus = 1.0;
}
if(mean === undefined) {
mean = (min + max) / 2.0;
}
if(focus == 0) {
return d3.randomUniform(min, max)();
}
else if(focus < 0) {
focus = -1 / focus;
}
sigma = (max - mean) / focus;
val = d3.randomNormal(mean, sigma)();
if (val > min && val < max) {
return val;
}
return d3.randomUniform(min, max)();
}
var rndSeed;
var bot;
var renderReady = false;
function preload() {
bot = new bot();
bot.preload();
}
function setup () {
var main_canvas = createCanvas(440, 220);
main_canvas.parent('canvasContainer');
rndSeed = random(1024);
bot.setup();
}
function keyTyped() {
if (key == '!') {
saveBlocksImages();
}
else if (key == '@') {
saveBlocksImages(true);
}
}
function reportRenderReady() {
finalDiv = createDiv('(render ready)');
finalDiv.id("render_ready")
}
function draw() {
background(204);
// randomSeed(0);
resetFocusedRandom(rndSeed);
message = bot.respond();
var text = select('#tweet_text');
text.html(message);
if(renderReady == false) {
if(bot.isDone()) {
reportRenderReady();
renderReady = true;
}
}
}