Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <canvas id="drawing" width="600" height="600">Canvas not supported.</canvas>
  
<div class="selector border">
  <div class="select-box border" id="scale" >Размер:
    <button class="button" id="scale-up">Up </button>
    <button class="button" id="scale-down">Down</button>
  </div>
  <div class="select-box border" id="moving">Движение:
    <button class="button" id="left-move">Left</button>
    <button class="button" id="right-move">Right</button>
    <button class="button" id="up-move">Up</button>
    <button class="button" id="down-move">Down</button>
  </div>
  <div class="select-box border" id="rotation">Вращение:
    <button class="button" id="left-rotate">Left</button>
    <button class="button" id="right-rotate">Right</button>
  </div>
  <div class="select-box border" id="auto-center">Масштабирование:
    <button class="button" id="auto-center">AUTO</button>
</div>




              
            
!

CSS

              
                #drawing {
  border: 5px solid black;
  border-radius: 10%;
  background: white;  
  display: block;
  margin: 0 auto;
}

.border{
 font-size: 18px;
 border: 3px solid black;
 border-radius: 2%;
}

.selector{
    background: rgba(0, 255, 255, 0.6);
    padding: 10px 10px 10px 10px;
    margin-top: 1%;
    margin-left: 24%;
    width: 50%;
    height: 100%;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
}

.button{
    font: small-caps bold 12px/1 sans-serif;
    background-color: rgba(237, 85, 111, 1);
}

.select-box{
    padding: 5px 5px 5px 5px;
    font: small-caps bold 14px/1 sans-serif;
    gap: 5px;
    display: flex;
    flex-flow: column;

}
              
            
!

JS

              
                const obj = document.getElementById('drawing');
const ctx = obj.getContext('2d');

const pointStep = 0.01;
const W = obj.width;
const H = obj.height;

class Point{
    constructor(x,y){
      this.x = x;
      this.y = y;
      this.X = x;
      this.Y = y;
    }
  }
  
class Style{
  constructor(fillColor='black', strokeColor='black', lineWidth=2){
    this.fillColor = fillColor;
    this.strokeColor = strokeColor;
    this.lineWidth = lineWidth;
  }
}

function button(ctx, pointsArr, koef){
  let scaleVal = Math.min(W,H)/320;
  let angleVal = 0;

  // Scale buttons
  document.getElementById('scale-up').addEventListener('click', function() {
    scale(1.2, pointsArr[1]);
    scaleVal*=1/1.2;
    draw(ctx, pointsArr);
  });
  document.getElementById('scale-down').addEventListener('click', function() {
    scale(0.8, pointsArr[1]);
    scaleVal*=1/0.8
    draw(ctx, pointsArr);
  });

  // Moving buttons
  document.getElementById('left-move').addEventListener('click', function() {
    setPosition(-20, 0, pointsArr[1]);
    draw(ctx, pointsArr);
  });
  document.getElementById('right-move').addEventListener('click', function() {
    setPosition(20, 0, pointsArr[1]);
    draw(ctx, pointsArr);
  });
  document.getElementById('up-move').addEventListener('click', function() {
    setPosition(0, -20, pointsArr[1]);
    draw(ctx, pointsArr);
  });
  document.getElementById('down-move').addEventListener('click', function() {
    setPosition(0, 20, pointsArr[1]);
    draw(ctx, pointsArr);
  });

  //Rotation buttons
  document.getElementById('right-rotate').addEventListener('click', function() {
    rotate(-15*koef, pointsArr[1]);
    angleVal-=15*koef;
    draw(ctx, pointsArr);
  });
  document.getElementById('left-rotate').addEventListener('click', function() {
    rotate(15*koef, pointsArr[1]);
    angleVal+=15*koef;
    draw(ctx, pointsArr);
  });

  // centering button
  document.getElementById('auto-center').addEventListener('click', function() {
    scale(scaleVal, pointsArr[1]);
    scaleVal = 1;
    rotate(-angleVal, pointsArr[1]);
    angleVal = 0;
    moveTo(W/2,H/2,pointsArr[1]);
    draw(ctx, pointsArr);
  });
}

function calcCenter(arr) {
  let sumX = 0;
  let sumY = 0;
  let count = 0;

  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; j++) {
      let point = arr[i][j];
      sumX += point.X;
      sumY += point.Y;
      count++;
    }
  }

  let cX = sumX / count;
  let cY = sumY / count;

  return new Point(cX, cY);
}

function makeLoop(X, Y, a, b, startAngle, endAngle){

  tmpArr = [];
  for (let i = startAngle*Math.PI/180; i <= endAngle*Math.PI/180;i+=pointStep){
    let x = a*Math.cos(i);
    let y = b*Math.sin(i);
    tmpArr.push(new Point(X+x,Y+y));
  }
  return tmpArr;
}

function moveTo(newX,newY,arr){
  let cPoint = calcCenter(arr);
  for(let i = 0; i < arr.length; i++){
    for(let j = 0; j < arr[i].length; j++){
      arr[i][j].X = arr[i][j].X - cPoint.X + newX;
      arr[i][j].Y = arr[i][j].Y - cPoint.Y + newY;
    }
  }
}

function setPosition(dX,dY,arr){
  for(let i = 0; i < arr.length; i++){
    for(let j = 0; j < arr[i].length; j++){
      arr[i][j].X = arr[i][j].X + dX;
      arr[i][j].Y = arr[i][j].Y + dY;
    }
  }
}

function scale(N, arr){
  let dX = calcCenter(arr).X*N - calcCenter(arr).X;
  let dY = calcCenter(arr).Y*N - calcCenter(arr).Y;

  for(let i = 0; i < arr.length; i++){
    for(let j = 0; j < arr[i].length; j++){
      arr[i][j].X = arr[i][j].X*N - dX;
      arr[i][j].Y = arr[i][j].Y*N - dY;
    }
  }
}

function rotate(degree, arr){
  const radians = degree*Math.PI/180;

  const cos = Math.cos(radians);
  const sin = Math.sin(radians);

  let cPoint = calcCenter(arr);

  for(let i = 0; i < arr.length; i++){
    for(let j = 0; j < arr[i].length; j++){
      const newX = (arr[i][j].X - cPoint.X) * cos - (arr[i][j].Y - cPoint.Y) * sin;
      const newY = (arr[i][j].X - cPoint.X) * sin + (arr[i][j].Y - cPoint.Y) * cos;

      arr[i][j].X = newX + cPoint.X;
      arr[i][j].Y = newY + cPoint.Y;
    }
  }
}

function draw(ctx, arr){
  ctx.fillStyle = 'rgb(0, 255, 255)';
  ctx.fillRect(0,0,W,H);

  let styleArr = arr[0];
  let pointsArr = arr[1];

  for(let i = 0; i < pointsArr.length; i++){
    ctx.beginPath();
    ctx.moveTo(pointsArr[i][0].X, pointsArr[i][0].Y);
    for(let j = 1; j < pointsArr[i].length; j++){
      ctx.lineTo(pointsArr[i][j].X, pointsArr[i][j].Y);
    }
    ctx.closePath();

    ctx.lineWidth = styleArr[i].lineWidth;
    ctx.fillStyle = styleArr[i].fillColor;
    ctx.strokeStyle = styleArr[i].strokeColor;

    ctx.fill();
    ctx.stroke();
  }
}

let pointsArr = [[],[]];

const eyeGrad = ctx.createLinearGradient(W/2, H/2-30, W/2, H);

eyeGrad.addColorStop(0, 'rgba(0, 194, 208, 1)');
eyeGrad.addColorStop(0.5, 'rgba(0, 236, 255, 1)');
eyeGrad.addColorStop(1,'rgba(186, 250, 255, 1)');

pointsArr[0].push(new Style(eyeGrad, 'black', 6)); // left eye main
pointsArr[1].push(makeLoop(W/2-50,H/2,20,28,120,423.5));

pointsArr[0].push(new Style(eyeGrad, 'black', 6)); // right eye main
pointsArr[1].push(makeLoop(W/2+50,H/2,20,28,120,423.5));

pointsArr[0].push(new Style('white', 'white', 4)); // left eye ball white
pointsArr[1].push(makeLoop(W/2-50,H/2+10,11,11,0,360));

pointsArr[0].push(new Style(eyeGrad, eyeGrad, 4)); // left eye ball little
pointsArr[1].push(makeLoop(W/2-50,H/2+5,9,9,0,360));  

pointsArr[0].push(new Style('white', 'white', 4)); // right eye ball white
pointsArr[1].push(makeLoop(W/2+50,H/2+10,11,11,0,360));

pointsArr[0].push(new Style(eyeGrad, 'rgba(255, 85, 143, 0)', 0)); // right eye ball little
pointsArr[1].push(makeLoop(W/2+50,H/2+5,9,9,0,360)); 

pointsArr[0].push(new Style('rgba(255, 85, 143, 1)', 'black', 4));
pointsArr[1].push([new Point(W/2-8,H/2+30), new Point(W/2+8,H/2+30), new Point(W/2+3,H/2+40), new Point(W/2-3,H/2+40)]); // mouth

pointsArr[0].push(new Style('rgba(255, 85, 143, 0.8)', 'rgba(255, 85, 143, 0.4)', 5)); // right pink ball
pointsArr[1].push(makeLoop(W/2+70,H/2+30,6,6,0,360)); 

pointsArr[0].push(new Style('rgba(255, 85, 143, 0.8)', 'rgba(255, 85, 143, 0.4)', 5)); // left pink ball
pointsArr[1].push(makeLoop(W/2-70,H/2+30,6,6,0,360)); 

pointsArr[0].push(new Style('white', 'white', 2));
pointsArr[1].push([new Point(W/2-48,H/2+3), new Point(W/2-48,H/2)]); // left eye smile one

pointsArr[0].push(new Style('white', 'white', 2));
pointsArr[1].push([new Point(W/2-52,H/2+3), new Point(W/2-52,H/2)]); // left eye smile two

pointsArr[0].push(new Style('white', 'white', 2));
pointsArr[1].push([new Point(W/2+48,H/2+3), new Point(W/2+48,H/2)]); // right eye smile one

pointsArr[0].push(new Style('white', 'white', 2));
pointsArr[1].push([new Point(W/2+52,H/2+3), new Point(W/2+52,H/2)]); // right eye smile two

pointsArr[0].push(new Style('black', 'black', 6)); // nose
pointsArr[1].push(makeLoop(W/2,H/2+20,1,1,0,360)); 

scale(2, pointsArr[1]);
draw(ctx, pointsArr);

button(ctx,pointsArr,1.5);
              
            
!
999px

Console