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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <canvas id="canvas"></canvas>
  <canvas id="canvas2"></canvas>
  <button onClick="getPic()">getPic</button>
  <p>获取的base64</p>
  <textarea id="text" cols="30" rows="10"></textarea>
  <p>获取的图片</p>
  <img id="pic"/>
</body>
</html>
              
            
!

CSS

              
                canvas{
  width:300px;
  height:300px;
  background:black;
}
              
            
!

JS

              
                const dpr=window.devicePixelRatio||2
const canvas=document.getElementById("canvas")
const context=canvas.getContext("2d")
const cWidth=300*dpr
const cHeight=300*dpr
const cap=50
const lineWidth=2
canvas.width=cWidth
canvas.height=cHeight

let Grids=[]
function initGrid(cap,width,height,lineWidth){
  const colNum=Math.ceil(width/cap)-1
  const rowNum=Math.ceil(height/cap)-1
  for(let i=1;i<=colNum;i++){
    Grids.push([[cap*i-1, 0,lineWidth,colNum*cap],[i*cap,cap*i-1,colNum*cap+5,"top","center"]]) 
  }
  for(let i=1;i<=rowNum;i++){
    Grids.push([[ 0,cap*i-1,rowNum*cap,lineWidth],[i*cap,rowNum*cap+5,cap*i-1,"middle","left"]])
  }
}
initGrid(cap,cWidth,cHeight,lineWidth);
function createGrid(){
  context.fillStyle = 'green';
  context.font="24px Arial"
  Grids.forEach((grid)=>{
    context.textAlign=grid[1][4]
     context.textBaseline=grid[1][3]
     context.fillRect( grid[0][0],grid[0][1], grid[0][2], grid[0][3])
     context.fillText (grid[1][0],grid[1][1], grid[1][2]);   
  })
}
createGrid()
//获取canvas图片
const text=document.getElementById("text")
const pic=document.getElementById("pic")
function getPic(){
  let dataUrl = canvas.toDataURL('image/jpeg');  
  text.value=dataUrl
  pic.src=dataUrl
}
//canvas2
const canvas2=document.getElementById("canvas2")
const context2=canvas2.getContext("2d")
const cap2=50
canvas2.width=300
canvas2.height=300
let Grids2=[]
function initGrid2(cap,width,height,lineWidth){
  const colNum=Math.ceil(width/cap)-1
  const rowNum=Math.ceil(height/cap)-1
  for(let i=1;i<=colNum;i++){
    Grids2.push([[cap*i-1, 0,lineWidth,colNum*cap],[i*cap,cap*i-1,colNum*cap+5,"top","center"]]) 
  }
  for(let i=1;i<=rowNum;i++){
    Grids2.push([[ 0,cap*i-1,rowNum*cap,lineWidth],[i*cap,rowNum*cap+5,cap*i-1,"middle","left"]])
  }
}
initGrid2(cap2,300,300,lineWidth);
function createGrid2(){
  context2.fillStyle = 'green';
  context2.font="24px Arial"
  Grids2.forEach((grid)=>{
    context2.textAlign=grid[1][4]
     context2.textBaseline=grid[1][3]
     context2.fillRect( grid[0][0],grid[0][1], grid[0][2], grid[0][3])
     context2.fillText (grid[1][0],grid[1][1], grid[1][2]);   
  })
}
createGrid2()
              
            
!
999px

Console