@@ -538,6 +538,90 @@ <h3>New to C++ Mode?</h3>
538538</ footer >
539539
540540< script src ="./assets/nav.js "> </ script >
541+ < link rel ="stylesheet " href ="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.css ">
542+ < link rel ="stylesheet " href ="/assets/cppmode-theme.css ">
543+ < script src ="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.js "> </ script >
544+ < script src ="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/mode/clike/clike.min.js "> </ script >
545+ < script src ="/assets/cppmode-keywords.js "> </ script >
546+ < script src ="/assets/cppmode.js "> </ script >
547+ < script >
548+ const HERO_API = 'https://bench-volumes-pontiac-riders.trycloudflare.com' ;
549+ const HERO_DEFAULT = `enum PartType { HALF_O, PLUS, PLUS_SHADOW };
550+ struct Part {
551+ PartType type;
552+ float x, y, rot, r, t, s;
553+ color col;
554+ };
555+ Part parts[] = {
556+ {PLUS_SHADOW, 411,340,-0.5f,0,55,170,color(190,145,85)},
557+ {HALF_O, 261,344, 0, 180,70,0, color(255,210,60)},
558+ {PLUS, 319,348,-0.5f,0,55,170,color(255,210,60)},
559+ };
560+ void drawHalfO(Part& p) {
561+ pushMatrix(); translate(p.x,p.y); rotate(p.rot);
562+ noStroke(); fill(p.col); beginShape();
563+ for (float a=radians(90);a<=radians(270);a+=0.02f)
564+ vertex(cos(a)*p.r, sin(a)*p.r);
565+ for (float a=radians(270);a>=radians(90);a-=0.02f)
566+ vertex(cos(a)*(p.r-p.t), sin(a)*(p.r-p.t));
567+ endShape(CLOSE); popMatrix();
568+ }
569+ void drawPlus(Part& p, float o) {
570+ pushMatrix(); translate(p.x,p.y); rotate(p.rot);
571+ rectMode(CENTER); noStroke(); fill(p.col);
572+ rect(o,o,p.t,p.s,10); rect(o,o,p.s,p.t,10);
573+ popMatrix();
574+ }
575+ void setup() { size(600,700); noLoop(); }
576+ void draw() {
577+ background(255);
578+ for (int i=0;i<3;i++) {
579+ if (parts[i].type==HALF_O) drawHalfO(parts[i]);
580+ if (parts[i].type==PLUS_SHADOW) drawPlus(parts[i],6);
581+ if (parts[i].type==PLUS) drawPlus(parts[i],0);
582+ }
583+ }` ;
584+
585+ const heroEditor = CodeMirror . fromTextArea ( document . getElementById ( 'hero-editor' ) , {
586+ mode : 'cppmode' , theme : 'cppmode' , lineNumbers : true ,
587+ indentUnit : 2 , tabSize : 2 , autofocus : false ,
588+ } ) ;
589+ heroEditor . setValue ( HERO_DEFAULT ) ;
590+ heroEditor . setSize ( '100%' , '320px' ) ;
591+
592+ function heroLaunch ( html ) {
593+ const area = document . getElementById ( 'hero-preview' ) ;
594+ const old = area . querySelector ( 'iframe' ) ;
595+ const iframe = document . createElement ( 'iframe' ) ;
596+ iframe . setAttribute ( 'sandbox' , 'allow-scripts allow-same-origin' ) ;
597+ iframe . style . cssText = 'position:absolute;top:0;left:0;width:600px;height:700px;border:none;transform-origin:top left;transform:scale(0.533);' ;
598+ iframe . src = URL . createObjectURL ( new Blob ( [ html ] , { type :'text/html' } ) ) ;
599+ if ( old ) { area . replaceChild ( iframe , old ) ; try { URL . revokeObjectURL ( old . src ) ; } catch ( e ) { } }
600+ else area . appendChild ( iframe ) ;
601+ }
602+
603+ document . getElementById ( 'hero-run-btn' ) . addEventListener ( 'click' , async function ( ) {
604+ const btn = this ;
605+ btn . textContent = '...' ; btn . disabled = true ;
606+ try {
607+ const resp = await fetch ( HERO_API + '/compile' , {
608+ method : 'POST' , headers : { 'Content-Type' :'application/json' } ,
609+ body : JSON . stringify ( { code : heroEditor . getValue ( ) } ) ,
610+ } ) ;
611+ const data = await resp . json ( ) ;
612+ if ( data . error ) { alert ( 'Error:\n' + data . error . substring ( 0 , 300 ) ) ; }
613+ else {
614+ const jsUrl = URL . createObjectURL ( new Blob ( [ data . js ] , { type :'application/javascript' } ) ) ;
615+ function b64 ( s ) { var b = atob ( s ) , a = new Uint8Array ( b . length ) ; for ( var i = 0 ; i < b . length ; i ++ ) a [ i ] = b . charCodeAt ( i ) ; return a ; }
616+ const wa = b64 ( data . wasm ) ;
617+ const da = data . data ? b64 ( data . data ) : new Uint8Array ( 0 ) ;
618+ const html = `<!DOCTYPE html><html><head><style>*{margin:0;padding:0;overflow:hidden}body{background:#fff}canvas{display:block}<\/style><\/head><body><canvas id="canvas"><\/canvas><script>(function(){window.Module={canvas:document.getElementById("canvas"),wasmBinary:new Uint8Array(${ JSON . stringify ( Array . from ( wa ) ) } ),getPreloadedPackage:function(){return new Uint8Array(${ JSON . stringify ( Array . from ( da ) ) } ).buffer;},print:console.log,printErr:console.warn};})();<\/script><script src="${ jsUrl } "><\/script><\/body><\/html>` ;
619+ heroLaunch ( html ) ;
620+ }
621+ } catch ( e ) { alert ( 'Server unreachable: ' + e . message ) ; }
622+ btn . textContent = '▶ Run' ; btn . disabled = false ;
623+ } ) ;
624+ </ script >
541625
542626</ body >
543627</ html >
0 commit comments