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>
  <section>
    <article>
         <canvas id="canvas"></canvas>
    </article>
    <div>
      <p>basic用法,画一个完整的图片</br>context.drawImage(image, 0,0,image.width,image.height);</p>
      <input type="file" name="getImage" id="getImage">
  <p>context.drawImage(image, 0,0,image.width/2,image.height/3);</p>
      <button onClick="imageTrasform()">变形</button>
  <p>context.drawImage(image, 200,100,image.width,image.height);</p>
  <button onClick="imagePosition()">位移</button>
   <p>context.drawImage(image, 200,100,image.width,image.height);</p>
  <button onClick="cutImage()">剪切</button>
  cutImage
    </div>
  </section>
</body>
</html>
              
            
!

CSS

              
                
section{
  display:flex
}
section div{
  flex-shrink:1;
}
article{
  flex-shrink:0;
  margin-right:10px;
}
canvas{
  width:600px;
  heigth:600px;
  background:#000;
}
              
            
!

JS

              
                let image=null;
const input = document.getElementById("getImage")
const dpr=10
const canvas=document.getElementById("canvas")
const context=canvas.getContext("2d")
const canvasWidth=300*dpr
const canvasHeight=300*dpr
const cap=50
const colNum=Math.ceil(canvasWidth/cap)-1
const rowNum=Math.ceil(canvasHeight/cap)-1
const lineWidth=2
canvas.width=canvasWidth
canvas.height=canvasHeight
//建立网格
function createGrid(){
  context.fillStyle = 'green';
  context.textAlign="center"
  context.font="24px Arial"
  context.textBaseline="top"
  for(let i=1;i<=colNum;i++){
    context.fillRect( cap*i-1, 0,lineWidth,colNum*cap)
    context.fillText (i*cap,cap*i-1,colNum*cap+5);   
  }
  context.textAlign="left"
  context.font="24px Arial"
  context.textBaseline="middle"
  for(let i=1;i<=rowNum;i++){
    context.fillRect( 0,cap*i-1,rowNum*cap,lineWidth)
    context.fillText (i*cap,rowNum*cap+5,cap*i-1);
  }
}
createGrid()

input.addEventListener("change",function(e){
	for (let i = 0;i < this.files.length;i++){
		getImageFormFile(this.files[i],renderImage); 
	}
})
function checkType(type){
	var r=false
	switch (type){
		case 'image/jpg':r=true;break;
		case 'image/png':r=true;break;
		case 'image/jpeg':r=true;break;
		case 'image/gif':r=true;break;
	}
	return r;
}
function getImageFormFile(file,callback){
	let reader = new FileReader();
	if(checkType(file.type)){
		reader.readAsDataURL(file);
	}
	reader.addEventListener('load',function () {
		if(checkType(file.type)){
			let img = new Image();
            img.src = reader.result;
            img.onload=function(){
              image=img
            	callback();
            }
            
		}
	});
}
function initImage(){
  context.clearRect(0,0,canvas.width,canvas.height);
  createGrid()
}
function renderImage(){
  initImage()
  context.drawImage(image, 0,0);
}
function imageTrasform(){
  initImage()
  context.drawImage(image, 100,100,image.width/2,image.height/3);
}
function imagePosition(){
  initImage()
  context.drawImage(image, 100,200,image.width,image.height);
}
function cutImage(){
  initImage()
  context.drawImage (image, 200, 0, image.width/2,image.height/2, image.width/2, 0,image.width/2,image.height/2);
}
              
            
!
999px

Console