p5 = function (
sketch,
estilo = "width: 100%; height: 400px;",
consolestyle = "display: none; overflow: hidden; width: calc(100% - 40px); padding: 20px; height: 0; background-color: black; color: white; font-family: Courier, monospace; font-size: 12px; line-height: 14px; position: fixed; top: 0; left: 0; z-index: 100;"
) {
var rex = /createCanvas\((.*),(.*)\)/i;
var tamanhos = rex.exec(sketch);
var ifw = tamanhos[1];
var ifh = tamanhos[2];
var revw = estilo.replace(/(width:.{2,10};)/i, "width: " + ifw + "px;");
var revh = revw.replace(
/(height:.{2,10};)/i,
"height: " + (parseInt(ifh) + 4) + "px;"
);
let iframe = html`<iframe frameborder="0" style="${revh}"
sandbox="allow-scripts allow-pointer-lock allow-same-origin allow-popups allow-forms allow-modals allow-downloads"></iframe>`;
iframe.srcdoc = `<!DOCTYPE HTML>
<html lang="en"><head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js" crossorigin=""></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/addons/p5.sound.min.js" crossorigin=""></script>
<meta charset="utf-8">
<style>
canvas, body, html {
padding:0;
margin:0;
}
</style>
</head>
<body style="margin:0;padding:0;"><script>${sketch}</script>
<div id="p5consoleoutput" style='${consolestyle}'></div>
<script>
console.defaultLog = console.log.bind(console);
console.logs = [];
p5.prototype.print = function(c) {
var c2 = c.replace("\\n","<br>");
document.getElementById('p5consoleoutput').style.display = 'block';
document.getElementById('p5consoleoutput').style.height = 'auto';
var a = document.getElementById('p5consoleoutput').innerHTML;
document.getElementById('p5consoleoutput').innerHTML = a + c2;
}
console.log = function () {
console.defaultLog.apply(console, arguments);
console.logs.push(Array.from(arguments));
if (arguments[0].match(/p5.js says/)) {
document.getElementById('p5consoleoutput').style.display = 'block';
document.getElementById('p5consoleoutput').style.height = 'auto';
var q = document.getElementById('p5consoleoutput').innerHTML;
if (q != "") {
document.getElementById('p5consoleoutput').innerHTML = q + "<br>" + arguments[0];
} else {
document.getElementById('p5consoleoutput').innerHTML = arguments[0];
}
}
}
</script>
</body></html>`;
return iframe;
}