Pen Settings



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


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


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.


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.


<figure class="apple-watch">
  <canvas id="watch" width="177" height="204"></canvas>
  <div id="dot"></div>
  <p id="user"></p>

  <h1>&#63743; watch</h1>
  <h2>Simulating the <em><a href="" target="_blank">Real Touch</em> Messaging</a> Sketch feature with realtime data streaming over HTML5 canvas, realtime powered by PubNub.</h2>

<p>Open this URL in another browser windows, and try drawing on the watchface!</p>

<p style="margin-top: 3em">Using <a href="" target="_blank">PubNub API</a> for the realtime messages. We have a full tutorial on building real touch technology in <a href="" target="_blank">our collaborative whiteboard HTML5 canvas tutorial and demo</a></p>



                * { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }
body {
    font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; 
    margin: 1em;
    color: #fff;
    background: #111 radial-gradient(circle farthest-side at 50% 8%, #555, #111) no-repeat;
header {
	margin-bottom: 1.5em;
h1, h2 {
	margin: 0;
	font-weight: normal;
h1 {
  font-size: 2.8em;
h2 {
  font-size: 1.4em;
a {
  color: lightblue;
canvas {
  /* border: 1px dotted silver;*/
#watch:active {
  cursor: pointer;

  text-align: center;

.apple-watch {
  width: 224px;
  height: 402px;
  display: inline-block;
  position: relative;
  background: url( top left no-repeat;

.apple-watch-sport {
  background: url( top left no-repeat;
  margin-left: 100px;

.apple-watch > canvas {
  position: absolute;
  top: 98px;
  left: 18px;

#user {
  position: absolute;
  top: 260px;
  left: 26px;
  font-size: 0.9em;
  color: silver;
  opacity: 0.75;

#dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 3px solid #45D0EC;
  position: absolute;
  top: 120px;
  left: 104px;
  display: none;


                (function() {
  if(Math.floor(Math.random()*10)%2 === 0){

/* PubNub */

var channel = 'draw-apple-watch';

var uuid = PUBNUB.uuid();

document.getElementById('user').textContent = 'user: ' +  uuid.substr(uuid.length - 17);

var pubnub = PUBNUB.init({
		publish_key: 'pub-c-156a6d5f-22bd-4a13-848d-b5b4d4b36695',
		subscribe_key: 'sub-c-f762fb78-2724-11e4-a4df-02ee2ddab7fe',
    uuid: uuid

  channel: channel,
  callback: drawFromStream,
  presence: function(m){
   console.log(m.occupancy + ' users');

/* Draw on canvas */

var canvas = document.getElementById('watch');
var ctx = canvas.getContext('2d');
ctx.lineWidth = '2';
ctx.lineCap = 'round';
ctx.lineJoin = 'round';

var gradient = ctx.createLinearGradient(0,0,canvas.width,0);
gradient.addColorStop('0', '#2EA6C7');
gradient.addColorStop('0.5', '#45D0EC');
gradient.addColorStop('1.0', '#155574');

ctx.strokeStyle = gradient;
ctx.shadowBlur = '3';
ctx.shadowColor = 'rgba(65, 212, 244, 0.2)';
ctx.globalAlpha = 0.75;

// no touch support for the CodePen demo 
canvas.addEventListener('mousedown', startDraw, false);
canvas.addEventListener('mousemove', draw, false);
canvas.addEventListener('mouseup', endDraw, false);

function drawOnCanvas(plots) {
  if (!plots) return;
  ctx.moveTo(plots[0].x, plots[0].y);

  for(var i=1; i<plots.length; i++) {
    ctx.lineTo(plots[i].x, plots[i].y);
  // Clear the canvas after 8 second
    ctx.clearRect(0, 0, canvas.width, canvas.height);
  }, 8000);

function drawFromStream(message) {
  if(!message || message.plots.length < 1) return;			

var isActive = false;
var plots = [];
var dot = document.getElementById('dot');

function draw(e) {
  if(!isActive) return;

  var x = e.offsetX || e.layerX - canvas.offsetLeft + 10;
  var y = e.offsetY || e.layerY - canvas.offsetTop + 100;

  plots.push({x: x, y: y});
function startDraw(e) {
  isActive = true; = 'block';
function endDraw(e) {
  isActive = false; = 'none';
    channel: channel,
    message: {
      plots: plots

  plots = [];