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="canvas" width="280" height="150"></canvas>

              
            
!

CSS

              
                * {
    margin: 0;
    padding: 0;
}
canvas {
    border: 1px solid orange;
    margin: 10px auto 0;
    display: block;
}
              
            
!

JS

              
                function KeyPress() {
    this.keyListeners = []
}
KeyPress.prototype = {
    addKeyListener: function(keyAndListener) {
        this.keyListeners.push(keyAndListener)
    },
    findKeyListener: function(key) {
        var listener = undefined
        this.keyListeners.forEach(function(keyAndListener) {
            var currentKey = keyAndListener.key
            if(currentKey === key) {
                listener = keyAndListener.listener
            }
        })
        return listener
    },
    keyPressed: function(e) {
        var listener = undefined,
            key = undefined
        switch (e.keyCode) {
            case 32: key = 'space'; break;
            case 37: key = 'left'; break;
            case 39: key = 'right'; break;
            case 38: key = 'up'; break;
            case 40: key = 'down'; break;
        }
        listener = this.findKeyListener(key)
        if(listener) {
            listener()
        }
    }
}

// 业务代码.......
var canvas = document.getElementById('canvas')
	var context = canvas.getContext('2d')
	var isCollisions = false

	var Rectangle = function(context) {
		this.left = 0
		this.top = 0
		this.width = 50
		this.height = 50
		this.context = context
	}

	Rectangle.prototype = {
		paint: function() {
			context.fillRect(this.left, this.top, this.width, this.height)
		}
	}

	var rect1 = new Rectangle(context)
	var rect2 = new Rectangle(context)


	function handleEgdeCollisions(rect1, rect2) {
		if(rect1.left < rect2.left + rect2.width &&
	  	rect1.left + rect1.width > rect2.left &&
	  	rect1.top < rect2.top + rect2.height &&
	  	rect1.height + rect1.top > rect2.top) {
			console.log('撞了')
      return true
		} else {
			console.log('没撞')
			return false
		}
	}

	function draw() {
		context.clearRect(0, 0, canvas.width, canvas.height)

		context.save()
		context.fillStyle = 'cornflowerblue';
		context.font = '24px Arial';
		if(isCollisions) {
      context.fillText('collision', 15, canvas.height - 10);
		} else {
			context.fillText('通过↑↓←→键移动元素', 15, canvas.height - 10)
		}
		context.restore()

		context.save()
		context.fillStyle = 'red'
		rect2.left = 70
		rect2.top = 50
		rect2.paint()
		context.restore()
		rect1.paint()
	}

	function keyPressHandle(e) {
    e.preventDefault()
		keyPress.keyPressed(e)
		isCollisions = handleEgdeCollisions(rect1, rect2)
		draw()
	}

	window.addEventListener('keypress', keyPressHandle, false)
	window.addEventListener('keydown',  keyPressHandle, false)

	draw()

	var keyPress = new KeyPress()
	keyPress.addKeyListener({
		key: 'right',
		listener: function() {
			rect1.left += 2
		}
	})

	keyPress.addKeyListener({
		key: 'left',
		listener: function() {
			rect1.left -= 2
		}
	})

	keyPress.addKeyListener({
		key: 'up',
		listener: function() {
			rect1.top -= 2
		}
	})

	keyPress.addKeyListener({
		key: 'down',
		listener: function() {
			rect1.top += 2
		}
	})
              
            
!
999px

Console