css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

  h3.header Modular Multiplication

      h3 Options
        label(for="modulo").col-sm-4 Modulo
        input(type="number" id="modulo" step="1").col-sm-8.form-control
        label(for="coef").col-sm-4 Multiplier
        input(type="number" id="coef" step="0.1").col-sm-8.form-control
            input(type="checkbox" id="autoAlpha").custom-control-input
            span.custom-control-description Automatically adjust alpha
        label(for="").col-sm-4 View
            input(type="checkbox" id="drawCircle").custom-control-input
            span.custom-control-description Circle
            input(type="checkbox" id="drawPoints").custom-control-input
            span.custom-control-description Points
            input(type="checkbox" id="drawLines").custom-control-input
            span.custom-control-description Lines
            input(type="checkbox" id="drawText").custom-control-input
            span.custom-control-description Text
            input(type="checkbox" id="drawTitle").custom-control-input
            span.custom-control-description Title
        label(for="res").col-sm-4 Resolution
        input(type="number" id="res" step="20").col-sm-8.form-control
        label(for="export").col-sm-4 Save this!
        input(type="button" id="export" value="Export").btn.btn-primary.col-sm-4
          .small More options if you can find them in the code! 😜
  h3 How does this work?
      img(src="https://image.ibb.co/jwaoF6/01.png", alt="").img-fluid
      p Draw ten points evenly spread around a circle.
      img(src="https://image.ibb.co/cVYx8R/02.png", alt="").img-fluid
      p Multiply each point by two. Take the remainder of the division of the result by ten, and draw a line between the two points.
      img(src="https://image.ibb.co/iRFYhm/03.png", alt="").img-fluid
      p Here is the same operation, but for 50 points.
      img(src="https://image.ibb.co/dNFoF6/04.png", alt="").img-fluid
      p Hiding the points and text, we see a "nice" shape emerge.
      img(src="https://image.ibb.co/j9hqTR/05.png", alt="").img-fluid
      p Try it out with higher or even negative multipliers!
  h3 Some examples...
    - var urls = ['https://image.ibb.co/fcjtnm/example0.png', 'https://image.ibb.co/ja9R7m/example1.png', 'https://image.ibb.co/geEtnm/example2.png', 'https://image.ibb.co/jWU4tR/example3.png', 'https://image.ibb.co/dzjF06/example4.png', 'https://image.ibb.co/cNh2f6/example5.png'];
    each url in urls
        img(src=url, alt="").img-fluid
  h3 About  
  p Inspired by this 
    a(href="https://www.youtube.com/watch?v=-X49VQgi86E") video
  p Made with 💗 by 
    a(href="codepen.io/ninivert") ninivert
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;

body {
  $base-font-size: 18px;
  $heading-scale: 8;
  @for $i from 1 through 6 {
    h#{$i} {
      margin: .5em 0;
      font-size: $base-font-size + $heading-scale * (6 - $i);
      color: #2c3e50;
  color: #34495e;
  .header {
    margin-top: .4em;
    padding: .4em 0;
    position: relative;
    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: 0;
      display: block;
      width: 2em;
      height: 2px;
      background: #2980b9;
    // border-bottom: 2px solid #2980b9;
  .row {
    > #side {
      > .row {
        margin-bottom: .5em;
  #container {
    // overflow: auto;
    > canvas {
      width: 100% !important;
      height: auto !important;
              function init() {
  window.c = document.createElement('canvas');
  c.width = c.height = 500;
  window.ctx = c.getContext('2d');
  ctx.translate(c.width/2, c.height/2); // center around the center of the circle
  // Options
  window.opts = {
    modulo: 1000, // modulo
    coef: 2, // multiplier
    autoAlpha: true, // adjust the alpha depending on the number of points
    drawCircle: true,
    drawPoints: false,
    drawLines: true,
    drawText: false,
    drawTitle: true,
    res: Math.min(c.width, c.height), // resolution
    export: null, // export button placeholder
    ENDOFOPTIONS: null, // Placeholder
    radius: Math.min(c.width/2, c.height/2)-80, // radius of the big circle
    pointRadius: 4, // radius of the points
    circleColor: '#cccccc',
    pointColor: '#16a085',
    lineColor: '#222222',
    textColor: '#1abc9c',
    titleColor: '#2c3e50',
    bgColor: '#ffffff',
    elmts: {},
    handlers: {
      modulo: function() {
        let val = Math.max(this.value, 1);
        opts.modulo = val;
        this.value = val;
      coef: function() {
        let val = this.value;
        opts.coef = val;
        this.value = val;
      autoAlpha: function() {
        opts.autoAlpha = this.checked;
      drawCircle: function() {
        opts.drawCircle = this.checked;
      drawPoints: function() {
        opts.drawPoints = this.checked;
      drawLines: function() {
        opts.drawLines = this.checked;
      drawText: function() {
        opts.drawText = this.checked;
      drawTitle: function() {
        opts.drawTitle = this.checked;
      res: function() {
        // Retranslate the canvas back to (0, 0)
        ctx.translate(-c.width/2, -c.height/2);
        // Adjust the variables that depend on resolution
        let val = Math.max(this.value, 200);
        c.width = c.height = val;
        opts.res = val;
        opts.radius = val/2-80;
        // Apply new translation
        ctx.translate(val/2, val/2);
        // Update and render
        this.value = val;
      export: function() {
        let link = document.createElement('a');
        link.download = `Modular multiplication of ${opts.coef} modulo ${opts.modulo}`;
        link.href = c.toDataURL('image/png');
        // Need to add the link to the body for Firefox
        link.setAttribute("type", "hidden");
    init: function() {
      let keys = Object.keys(this),
          elmt, type;

      for (let i of keys) {
        // Stop when you arrive at the end of the option list
        if (i === 'ENDOFOPTIONS') break;

        this.elmts[i] = document.getElementById(i);
        elmt = this.elmts[i];
        type = elmt.type;

        if (type === 'checkbox' || type === 'button') {
          elmt.checked = this[i];
          elmt.onclick = this.handlers[i];
        if (type === 'number') {
          elmt.value = this[i];
          elmt.onchange = this.handlers[i];

function draw() {
   * Utility functions
   * Get the index of the point to bind to
   * Get an angle from a point relative to the center of the circle
     -Math.PI/2 to have point 0 facing up
   * Get the of a point from said angle and radius
  let getBindingPoint = n => n*opts.coef%opts.modulo;
  let getAngle = ratio => ratio*2*Math.PI-Math.PI/2;
  let getCoords = (a, r) => ({x: Math.cos(a)*r, y: Math.sin(a)*r});
  // Clear the canvas
  ctx.globalAlpha = 1;
  ctx.fillStyle = opts.bgColor;
  ctx.fillRect(-c.width/2, -c.height/2, c.width, c.height);
  // Draw external circle
  if (opts.drawCircle) {
    ctx.arc(0, 0, opts.radius, 2*Math.PI, 0);
    ctx.strokeStyle = opts.circleColor;
  // Draw title
  if (opts.drawTitle) {
    ctx.textAlign = 'left';
    ctx.textBaseline = 'top';
    ctx.font = 'italic 16pt Arial';
    ctx.fillStyle = opts.titleColor;
    ctx.fillText(`Modular multiplication of ${opts.coef} modulo ${opts.modulo}`, -c.width/2+20, -c.height/2+20, c.width-40);
  let point, bindingPoint, textPoint;

  // Draw points, lines and text
  for (let i=0; i<opts.modulo; i++) {
    // Draw the point
    point = getCoords( getAngle(i/opts.modulo), opts.radius );
    if (opts.drawPoints) {
      ctx.arc(point.x, point.y, opts.pointRadius, 2*Math.PI, 0);
      ctx.fillStyle = opts.pointColor;
    // Draw the line
    if (opts.drawLines) {
      bindingPoint = getCoords( getAngle(getBindingPoint(i)/opts.modulo), opts.radius );
      if (opts.autoAlpha) ctx.globalAlpha = Math.min(opts.res*.2/opts.modulo, 1);
      ctx.moveTo(point.x, point.y);
      ctx.lineTo(bindingPoint.x, bindingPoint.y);
      ctx.strokeStyle = opts.lineColor;
      ctx.globalAlpha = 1;
    // Draw the text
    if (opts.drawText) {
      textPoint = getCoords( getAngle(i/opts.modulo), opts.radius*1.08 );
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      ctx.font = '12pt Arial'
      ctx.fillStyle = opts.textColor;
      ctx.fillText(i, textPoint.x, textPoint.y);

🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................