xxxxxxxxxx
//this code doesn't work in laptop .please use cellphone.
let grid;
let cols;
let rows;
let resolution = 5;
let buttontakePhoto;
let buttonsavePhoto;
let photoSave;
let modo = 0;
let confirmPhoto = false;
let mic;
var video;
let mySound;
let capture ;
let value = 0;
let switchFlag = false;
let switchBtn;
let x = window.screen.width;
let y = window.screen.height;
var options= { //スマホ用にカメラを切り替え
audio: false,
video: {
facingMode: {
exact: "environment" //previous:environment
}
}
};
function make2DArray(cols, rows) {
let arr = new Array(cols);
for (let i = 0; i < arr.length; i++) {
arr[i] = new Array(rows);
}
return arr;
}
function setup() {
let c = createCanvas(500, 700);
//c.mousePressed(canvasPressed);
bx = width / 2.0;
by = height-150 ;
rectMode(RADIUS);
strokeWeight(2);
cols = width / resolution;
rows = height / resolution;
capture = createCapture(options);
capture.hide()
switchBtn = createButton('Switch Camera');
switchBtn.position(19, 19);
switchBtn.mousePressed(switchCamera);
grid = make2DArray(cols, rows);
for (let i = 0; i < cols; i++) { //cols/rowsについて外部環境から何か数値を入れることで調整したい
for (let j = 0; j < rows; j++) {
grid[i][j] = floor(random(2));
}
}
}
function switchCamera()
{
switchFlag = !switchFlag;
if(switchFlag==true)
{
capture.remove();
options = {
audio:false,
video: {
facingMode: {
exact: "user"
}
}
};
}
else
{
capture.remove();
options = {
video:{
facingMode:{
exact:"environment"
}
}
};
}
capture = createCapture(options);
}
function draw() {
background(0);
image(capture,0,0,500,700);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
let x = i * resolution;
let y = j * resolution;
if (grid[i][j] == 1) { //要変更
fill(random(255),random(255),j);
noStroke();
ellipse(x, y,resolution+1, resolution +1);
}
}
}
let next = make2DArray(cols, rows);
// Compute next based on grid
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
let state = grid[i][j];
// Count live neighbors!
let sum = 0;
let neighbors = countNeighbors(grid, i, j);
if (state == 0 && neighbors == 3) {
next[i][j] = 1;
} else if (state == 1 && (neighbors < 2 || neighbors > 3 ) ) {
next[i][j] = 0;
} else {
next[i][j] = state;
}
}
}
grid = next;
}
function countNeighbors(grid, x, y) {
let sum = 0;
for (let i = -1; i < 2; i++) {
for (let j = -1; j < 2; j++) {
let col = (x + i + cols) % cols;
let row = (y + j + rows) % rows;
sum += grid[col][row];
}
}
sum -= grid[x][y];
return sum;
}