Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<html>
  <body>
  <div style="position:relative">
  <canvas style="touch-action: none; position:absolute;" id="canvas1"></canvas>
  </div>
  <br>
  Click and hold or press and hold to draw in the space above.
  <button onclick="clear_canvas()">Clear</button>
</body>
</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                // Original code from https://codepen.io/danielecastagna/full/MBEaE
// All credits to @danielecastagna, all bugs introduced thereafter
// would be mine.
// Fixed even further by wfurr@chromium.org

const VERTEX_SHADER = `
precision mediump float;
attribute vec3 a_Position;
varying vec4 vcolor;
void main() {
  vcolor = vec4(0.0, 1.0, 0.0, 1.0);
  gl_Position = vec4(a_Position.x, -a_Position.y, 0.0, 1.0);
}`;

 // Fragment shader program
const FRAGMENT_SHADER = `
precision mediump float;
varying vec4 vcolor;
void main() {
  gl_FragColor = vcolor;
}`;


const canvas = document.querySelector("canvas");
const gl = canvas.getContext("webgl", {desynchronized: true, alpha: false});
const dpr = devicePixelRatio;
const angle = screen.orientation.angle % 360;

canvas.style.transform = `rotateZ(${angle}deg)`;      
canvas.style.border = `${1 / dpr}px solid black`;

const integerWidth = Math.floor(500 * 0.9);
const integerHeight = Math.floor(300 * 0.9);

if (angle % 180 == 90) {
  console.log('canvas rotated 90deg');
  canvas.width = integerHeight;
  canvas.height = integerWidth;

  width = integerHeight / dpr;
  height = integerWidth / dpr;

  canvas.style.width = `${integerHeight / dpr}px`;
  canvas.style.height = `${integerWidth / dpr}px`;  

  const offset = (height - width) / 2;
  console.log('canvas offset left ' + offset + ', top -' + offset);
  canvas.style.left = `${offset}px`;
  canvas.style.top = `-${offset}px`;
} else {
  canvas.width = integerWidth;
  canvas.height = integerHeight;

  width = integerWidth / dpr;
  height = integerHeight / dpr;

  canvas.style.width = `${integerWidth / dpr}px`;
  canvas.style.height = `${integerHeight / dpr}px`;  
}

console.log('canvas dimensions: ' + width + ' ' + height)
console.log('border width: ' + (1 / dpr))
clear_canvas();

gl.viewport(0, 0, width, height);
const vertex_shader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertex_shader, VERTEX_SHADER);
gl.compileShader(vertex_shader);
if (!gl.getShaderParameter(vertex_shader, gl.COMPILE_STATUS)) { console.error("failed to compile vertex" + gl.getShaderInfoLog(vertex_shader)); }

const frag_shader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(frag_shader, FRAGMENT_SHADER);
gl.compileShader(frag_shader);
if (!gl.getShaderParameter(frag_shader, gl.COMPILE_STATUS)) { console.error("failed to compile fragment" + gl.getShaderInfoLog(frag_shader)); }

const program = gl.createProgram();
gl.attachShader(program, vertex_shader);
gl.attachShader(program, frag_shader);
gl.linkProgram(program);

if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
   console.error("Could not initialise shaders " + gl.getProgramInfoLog(program)); 
}
gl.useProgram(program);

function getPoint(e) {
  const rect = canvas.getBoundingClientRect();
  return { x: e.clientX - rect.left, y: e.clientY - rect.top };
}

let point;
canvas.addEventListener("pointerdown", (e) => {
  point = getPoint(e);
});
canvas.addEventListener("pointermove", (e) => {
  if (point) {
    const events = e.getCoalescedEvents();
    events.forEach((e) => {
      const new_point = getPoint(e);
                 
      const buffer = gl.createBuffer();
      gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
      const start_x = (point.x / width) * 2 - 1;
      const start_y = (point.y / height) * 2 - 1;
      const end_x = (new_point.x / width) * 2 - 1;;
      const end_y = (new_point.y / height) * 2 - 1;;
     
      const vertices = [
          start_x,  start_y, 0.0,
          start_x + .01, start_y + .01,  0.0,
          end_x,  end_y,  0.0,
          end_x + .01,  end_y + .01,  0.0,
      ];
      
      gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices) , gl.DYNAMIC_DRAW); 
      const pos_attr = gl.getAttribLocation(program, "a_Position");
      gl.enableVertexAttribArray(pos_attr);
      gl.vertexAttribPointer(pos_attr, 3, gl.FLOAT, false, 0, 0);
      
      gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
      gl.deleteBuffer(buffer);
      gl.flush();
      point = new_point;
    });
  }
});
canvas.addEventListener("pointerup", (e) => {
  point = null;
});

function clear_canvas() {
  gl.clearColor(1.0, 1.0, 1.0, 1.0);
  gl.clear(gl.COLOR_BUFFER_BIT);
}
              
            
!
999px

Console