cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <canvas id="mainCanvas" width="800" height="300"></canvas>
            
          
!
            
              * {
  margin: 0;
  padding: 0;
  background: #241C1F;
}
canvas {
  margin: 0 auto;
  display: block;
  border-left: 0;
  border-right: 0;
}
            
          
!
            
              // twitter @msvaljek
// https://msvaljek.blogspot.com/2013/06/canvas-wipers.html

// wipers

// standard shim
window.requestAnimFrame = (function(){
	return window.requestAnimationFrame ||
	window.webkitRequestAnimationFrame ||
	window.mozRequestAnimationFrame ||
	function( callback ) {
		window.setTimeout(callback, 1000 / 60);
	};
})();

function distance(p1X, p1Y, p2X, p2Y) {
	return Math.sqrt(Math.pow(p1X - p2X, 2) + Math.pow(p1Y - p2Y, 2));
}

// dom
var canvas = document.getElementById('mainCanvas');
var ctx = canvas.getContext('2d');

var engineSpeed = -0.05,
	engineAngle = 0,
	engineX = Math.floor(canvas.width * 0.6);
	engineR = 10,
	engineY = Math.floor(canvas.height - 2 * engineR),
	leftWiperX = Math.floor(engineX - canvas.height / 2),
	leftWiperLowerHandle = 11,
	leftWiperY = engineY - leftWiperLowerHandle,
	leftShaftLength = leftWiperX - engineX - leftWiperLowerHandle + engineR,
	rightWiperX = Math.floor(engineX + canvas.height / 7),
	rightWiperLowerHandle = 13,
	rightWiperY = engineY - leftWiperLowerHandle,
	rightShaftLength = rightWiperX - engineX,
	wiperCarrierLength = 100,
	wiperSubCarrierLength = 20,
	subCarrierAngle = Math.PI / 4,
	wiperFlipLowerRatio = 0.3,
	wiperFlipUpperRatio = 1.7,
	jointR = 2,
	lineWidth = 3;

(function animloop(){
	requestAnimFrame(animloop);
	ctx.clearRect(0, 0, canvas.width, canvas.height);
	ctx.lineWidth = lineWidth;

	// motor
	engineAngle = (engineAngle + engineSpeed) % (2 * Math.PI);
	ctx.fillStyle = '#8C8F4D';
	ctx.strokeStyle = '#E2E868';
	ctx.beginPath();
	ctx.arc(engineX, engineY, engineR, 0, 2 * Math.PI);
	ctx.fill();
	ctx.beginPath();
	ctx.moveTo(engineX, engineY);
	var crankX = engineX + Math.cos(engineAngle) * engineR,
		crankY = engineY + Math.sin(engineAngle) * engineR;
	ctx.lineTo(crankX, crankY);
	ctx.stroke();
	ctx.beginPath();
	ctx.arc(crankX, crankY, jointR, 0, 2 * Math.PI);
	ctx.closePath();
	ctx.stroke();

	// left wiper begin
	ctx.beginPath();
	ctx.arc(leftWiperX, leftWiperY, leftWiperLowerHandle, 0, 2 * Math.PI);
	ctx.fill();

	var dx = crankX - leftWiperX;
	var dy = crankY - leftWiperY;
	var d = distance(crankX, crankY, leftWiperX, leftWiperY);
	var a = ((leftWiperLowerHandle * leftWiperLowerHandle) - (leftShaftLength * leftShaftLength) + (d * d)) / (2.0 * d) ;
	var x2 = leftWiperX + (dx * a / d);
	var y2 = leftWiperY + (dy * a / d);
	var h = Math.sqrt((leftWiperLowerHandle * leftWiperLowerHandle) - (a * a));
	var rx = -dy * (h / d);
	var ry = dx * (h / d);
	var joinLeftX = x2 + rx;
	var jointLeftY = y2 + ry;

	// left shaft
	ctx.beginPath();
	ctx.moveTo(crankX, crankY);
	ctx.lineTo(joinLeftX, jointLeftY);
	ctx.closePath();
	ctx.stroke();
	ctx.beginPath();
	ctx.arc(joinLeftX, jointLeftY, jointR, 0, 2 * Math.PI);
	ctx.closePath();
	ctx.stroke();

	// left leaver
	ctx.beginPath();
	ctx.moveTo(joinLeftX, jointLeftY);
	ctx.lineTo(leftWiperX, leftWiperY);
	ctx.closePath();
	ctx.stroke();
	ctx.beginPath();
	ctx.arc(leftWiperX, leftWiperY, jointR, 0, 2 * Math.PI);
	ctx.closePath();
	ctx.stroke();

	var circledist = distance(leftWiperX - leftWiperLowerHandle, leftWiperY, joinLeftX, jointLeftY);
	var dAngleL = 2 * Math.acos(circledist / (2 * leftWiperLowerHandle)) + Math.PI * 4 / 5;
	var carrierLTopX = leftWiperX + Math.cos(dAngleL) * wiperCarrierLength;
	var carrierLTopY = leftWiperY + Math.sin(dAngleL) * wiperCarrierLength;

	var wiperFlipLTopX = leftWiperX + Math.cos(dAngleL) * (wiperCarrierLength * wiperFlipUpperRatio);
	var wiperFlipLTopY = leftWiperY + Math.sin(dAngleL) * (wiperCarrierLength * wiperFlipUpperRatio);
	var wiperFlipLBottomX = leftWiperX + Math.cos(dAngleL) * (wiperCarrierLength * wiperFlipLowerRatio);
	var wiperFlipLBottomY = leftWiperY + Math.sin(dAngleL) * (wiperCarrierLength * wiperFlipLowerRatio);

	// left carrier
	ctx.beginPath();
	ctx.moveTo(leftWiperX, leftWiperY);
	ctx.lineTo(carrierLTopX, carrierLTopY);
	ctx.closePath();
	ctx.stroke();

	// left sub carrier
	var subCarriersLdX = Math.cos(dAngleL + subCarrierAngle) * wiperSubCarrierLength;
	var subCarriersLdY = Math.sin(dAngleL + subCarrierAngle) * wiperSubCarrierLength;
	
	var subCarrierLX = carrierLTopX + subCarriersLdX;
	var subCarrierLY = carrierLTopY + subCarriersLdY;
	ctx.beginPath();
	ctx.moveTo(carrierLTopX, carrierLTopY);
	ctx.lineTo(subCarrierLX, subCarrierLY);
	ctx.closePath();
	ctx.stroke();
	ctx.beginPath();
	ctx.arc(carrierLTopX, carrierLTopY, jointR, 0, 2 * Math.PI);
	ctx.closePath();
	ctx.stroke();

	// left wiper
	ctx.beginPath();
	ctx.moveTo(wiperFlipLTopX + subCarriersLdX, wiperFlipLTopY + subCarriersLdY);
	ctx.lineTo(wiperFlipLBottomX + subCarriersLdX, wiperFlipLBottomY + subCarriersLdY);
	ctx.stroke();
	// left wiper end

	// right wiper begin
	ctx.beginPath();
	ctx.arc(rightWiperX, rightWiperY, rightWiperLowerHandle, 0, 2 * Math.PI);
	ctx.fill();

	dx = crankX - rightWiperX;
	dy = crankY - rightWiperY;
	d = distance(crankX, crankY, rightWiperX, rightWiperY);
	a = ((rightWiperLowerHandle * rightWiperLowerHandle) - (rightShaftLength * rightShaftLength) + (d * d)) / (2.0 * d) ;
	x2 = rightWiperX + (dx * a / d);
	y2 = rightWiperY + (dy * a / d);
	h = Math.sqrt((rightWiperLowerHandle * rightWiperLowerHandle) - (a * a));
	rx = -dy * (h / d);
	ry = dx * (h / d);
	var joinRightX = x2 - rx;
	var jointRightY = y2 - ry;

	// right shaft
	ctx.beginPath();
	ctx.moveTo(crankX, crankY);
	ctx.lineTo(joinRightX, jointRightY);
	ctx.closePath();
	ctx.stroke();
	ctx.beginPath();
	ctx.arc(joinRightX, jointRightY, jointR, 0, 2 * Math.PI);
	ctx.closePath();
	ctx.stroke();

	// right leaver
	ctx.beginPath();
	ctx.moveTo(joinRightX, jointRightY);
	ctx.lineTo(rightWiperX, rightWiperY);
	ctx.closePath();
	ctx.stroke();
	ctx.beginPath();
	ctx.arc(rightWiperX, rightWiperY, jointR, 0, 2 * Math.PI);
	ctx.closePath();
	ctx.stroke();

	circledist = distance(rightWiperX - rightWiperLowerHandle, rightWiperY, joinRightX, jointRightY);
	dAngleR = 2 * Math.acos(circledist / (2 * rightWiperLowerHandle)) + Math.PI * 4 / 5;
	var carrierRTopX = rightWiperX + Math.cos(dAngleR) * wiperCarrierLength;
	var carrierRTopY = rightWiperY + Math.sin(dAngleR) * wiperCarrierLength;

	var wiperFlipRTopX = rightWiperX + Math.cos(dAngleR) * (wiperCarrierLength * wiperFlipUpperRatio);
	var wiperFlipRTopY = rightWiperY + Math.sin(dAngleR) * (wiperCarrierLength * wiperFlipUpperRatio);
	var wiperFlipRBottomX = rightWiperX + Math.cos(dAngleR) * (wiperCarrierLength * wiperFlipLowerRatio);
	var wiperFlipRBottomY = rightWiperY + Math.sin(dAngleR) * (wiperCarrierLength * wiperFlipLowerRatio);

	// right carrier
	ctx.beginPath();
	ctx.moveTo(rightWiperX, rightWiperY);
	ctx.lineTo(carrierRTopX, carrierRTopY);
	ctx.closePath();
	ctx.stroke();
	ctx.beginPath();
	ctx.arc(carrierRTopX, carrierRTopY, jointR, 0, 2 * Math.PI);
	ctx.closePath();
	ctx.stroke();

	// right sub carrier
	var subCarriersRdX = Math.cos(dAngleR + subCarrierAngle) * wiperSubCarrierLength;
	var subCarriersRdY = Math.sin(dAngleR + subCarrierAngle) * wiperSubCarrierLength;
	
	var subCarrierRX = carrierRTopX + subCarriersRdX;
	var subCarrierRY = carrierRTopY + subCarriersRdY;
	ctx.beginPath();
	ctx.moveTo(carrierRTopX, carrierRTopY);
	ctx.lineTo(subCarrierRX, subCarrierRY);
	ctx.closePath();
	ctx.stroke();

	// right wiper
	ctx.beginPath();
	ctx.moveTo(wiperFlipRTopX + subCarriersRdX, wiperFlipRTopY + subCarriersRdY);
	ctx.lineTo(wiperFlipRBottomX + subCarriersRdX, wiperFlipRBottomY + subCarriersRdY);
	ctx.stroke();
	// right wiper end
})();
            
          
!
999px
Loading ..................

Console