Created
August 16, 2017 07:15
-
-
Save tosaka2/a58f746b4e5b7c3b9407d097decab9b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// return t.generate()の行にブレークポイント | |
getObject = () => t; | |
getState = () => getObject().state; | |
getOption = () => getState().options; | |
getNoise = () => getState().gan.noise; | |
// NoiseをFixedに | |
fixNoise = () => getOption().noise = 1; | |
// NoiseをRandomに | |
randomNoise = () => getOption().noise = 0; | |
// Noiseを出力 | |
printNoise = () => "[" + getNoise().join(',') + "]"; | |
// Noiseを設定 | |
setNoise = a => { cn = getNoise(); for (var i = 0; i < a.length; i++) cn[i]=a[i]; }; | |
// Generate | |
generate = async () => await getObject().generate(); | |
// NoiseをRandomにしてGenerate | |
generateByRandom = async () => { randomNoise(); await generate(); }; | |
// 引数で指定したNoiseでGenerate | |
generateBy = async a => { fixNoise(); setNoise(a); await generate() }; | |
// ベクトルの操作 | |
add = (a, b) => a.map((x, i) => x + b[i]); | |
sub = (a, b) => a.map((x, i) => x - b[i]); | |
times = (a, t) => a.map((x, i) => x * t); | |
interpolate = (a, b, p) => a.map((x, i) => x * (1-p) + b[i] * p); | |
_tmps = [[],[]]; | |
// 生成済みの画像を下に表示 | |
var addImg = function(src) { | |
var results = document.body.querySelector(".imgs"); | |
if (!results) { | |
results = document.createElement("div"); | |
results.className = "row imgs"; | |
document.body.querySelector(".App").appendChild(results); | |
} | |
var img = document.createElement("img"); | |
img.src = src; | |
var noise = getNoise(); | |
img.onclick = () => { | |
console.log("[" + noise.join(',') + "]"); | |
console.log(img); | |
img.style.opacity = 0.5; | |
if (_tmps[0][0] && !_tmps[1][0]) _tmps = [[noise, img], _tmps[0]]; | |
else { | |
if (_tmps[0][1]) _tmps[0][1].style.opacity = 1.0; | |
_tmps = [_tmps[1], [noise, img]]; | |
} | |
} | |
results.appendChild(img); | |
return img; | |
} | |
// 下に表示してある画像をクリア | |
var clearImgs = function() { | |
var imgs = document.body.querySelector(".imgs"); | |
for (var x of imgs.children) { | |
x.onclick = null; | |
x.src = ""; | |
} | |
imgs.parentNode.removeChild(imgs); | |
} | |
// 中断フラグ | |
var _isAborted = false; | |
// aとbに指定したNoiseでn枚画像補完し,下に表示.wait_secはGenerateにかかる時間(実行環境により調整). | |
var generateInterpolations = async function(n, a = _tmps[0][0], b = _tmps[1][0]) { | |
var img = document.body.querySelector(".result-canvas").firstChild; | |
var src = ""; | |
// デフォルト引数が使われたか | |
var ok = a == _tmps[0][0] && b == _tmps[1][0]; | |
if (ok) addImg(_tmps[0][1].src); | |
for (var i = ok ? 1 : 0; i < (ok ? n - 1 : n); i++) { | |
if (_isAborted) { | |
_isAborted = false; | |
break; | |
} | |
await generateBy(interpolate(a, b, i / (n - 1))); | |
addImg(img.src); | |
} | |
if (ok) addImg(_tmps[1][1].src); | |
} | |
// RandomなNoiseでn枚画像生成し,下に表示.wait_secはGenerateにかかる時間(実行環境により調整). | |
var generateRandomImages = async function (n) { | |
var result = document.body.querySelector(".result-canvas").firstChild; | |
var src = ""; | |
for (var i = 0; i <= n - 1; i++) { | |
if (_isAborted) { | |
_isAborted = false; | |
break; | |
} | |
await generateByRandom(); | |
addImg(result.src); | |
} | |
} | |
_bs = []; | |
// ボタン追加処理 | |
var addButton = function(text, func) { | |
var _b = document.body.querySelector(".btn-primary").cloneNode(); | |
_b.textContent = text; | |
_b.onclick = func; | |
document.body.querySelector(".options-container").lastChild.appendChild(_b); | |
_bs.push(_b); | |
} | |
addButton("生成!", () => generateRandomImages(100)); | |
addButton("補完!", () => generateInterpolations(10)); | |
addButton("中断!", () => { _isAborted = true; }); | |
addButton("複製!", () => { addImg(document.body.querySelector(".result-canvas").firstChild.src); }); | |
addButton("百合", () => generateInterpolations(3)); | |
addButton("クリア", () => clearImgs()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment