極客無極限 一行HTML5代碼引起的創意大爆炸

摘要:一行HTML5代碼能作什麼?國外開發者Jose Jesus Perez Aguinaga寫了一行HTML5代碼的文本編輯器。這件事在分享到Code Wall、Hacker News以後,引發了衆多開發者的注意,紛紛發表了本身的創意。

這是最初的HTML5代碼,它能夠運行在最新的Chrome和Firefox中,只需在瀏覽器地址欄輸入以下代碼:javascript

 

data:text/html, <html contenteditable>
可是功能十分有限,甚至沒有保存功能,樣式也很是簡陋。

 

因而,網友Montas修改了他的代碼,使用textarea標籤代替html標籤,能夠添加本身喜歡的樣式:css

 

data:text/html, <textarea style="font-size: 1.5em; width: 100%; height: 100%; border: none; outline: none" autofocus />
但bgrins依舊以爲不夠好看,繼續修改代碼,這段代碼會在你打字的時候改變「編輯器」的背景顏色,很是絢麗:
data:text/html, <html><head><link href='http://fonts.googleapis.com/css?family=Open+Sans'
    rel='stylesheet' type='text/css'><style type="text/css"> html
    { font-family: "Open Sans" } * { -webkit-transition: all linear 1s; }</style><script>window.onload=function(){var
    e=false;var t=0;setInterval(function(){if(!e){t=Math.round(Math.max(0,t-Math.max(t/3,1)))}var
    n=(255-t*2).toString(16);document.body.style.backgroundColor="#ff"+n+""+n},1e3);var
    n=null;document.onkeydown=function(){t=Math.min(128,t+2);e=true;clearTimeout(n);n=setTimeout(function(){e=false},1500)}}</script></head><body
    contenteditable style="font-size:2rem;line-height:1.4;max-width:60rem;margin:0
    auto;padding:4rem;">

網友jecxjo但願能有存儲功能:html

data:text/html,<button onClick="SaveTextArea()">Save</button> <script language="javascript" type="text/javascript"> function SaveTextArea() { window.location = "data:application/octet-stream," + escape(txtBody.value); } </script> <textarea id="txtBody" style="font-size: 1.5em; width: 100%; height: 100%; boarder: none; outline: none" autofocus> </textarea>

 

但上面的代碼是以文件形式存儲,samsonjs以爲不夠方便,並且須要點擊按鈕,因而添加了自動保存功能:java

data:text/html,<html lang="en"><head><style> html,body { height: 100% } #note { width: 100%; height: 100% } </style> <script> var note, html, timeout; window.addEventListener('load', function() { note = document.getElementById('note'); html = document.getElementsByTagName('html')[0]; html.addEventListener('keyup', function(ev) { if (timeout) clearTimeout(timeout); timeout = setTimeout(saveNote, 100); }); restoreNote(); note.focus(); }); function saveNote() { localStorage.note = note.innerText; timeout = null; } function restoreNote() { note.innerText = localStorage.note || ''; } </script> </head><body><h1>Notepad (type below, notes persist)</h1> <p id="note" contenteditable=""></p> </body></html>

如今但是雲時代!僅僅這樣怎能讓開發者止步?minikomi使用了第三方API打造了一個 在線編輯器:python

data:text/html,
<style type="text/css">
#e {
  position:absolute;
  top:0;
  right:0;
  bottom:0;
  left:0;
  font-size:16px;
}
</style>
<div id="e"></div>
<script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js"></script>
<script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
<script>
var myKey="SecretKeyz";
$(document).ready(function(){
    var e;
    var url = "http://api.openkeyval.org/"+myKey;
    $.ajax({
      url: url,
      dataType: "jsonp",
      success: function(data){
       e = ace.edit("e");
       e.setTheme("ace/theme/tomorrow_night_eighties");
       e.getSession().setMode("ace/mode/markdown");
       e.setValue(data);
      }
    });
 
    $("#e").on("keydown", function (b) {
      if (b.ctrlKey && 83 == b.which) {
        b.preventDefault();
        var data = myKey+"="+encodeURIComponent(e.getValue());
        $.ajax({
          data: data,
          url: "http://api.openkeyval.org/store/",
          dataType: "jsonp",
          success: function(data){
            alert("Saved.");
          }
        });
      }
    });
});
</script>

 

沒有代碼高亮功能的編輯器終究不適合程序員,Rails開發者jakeonrails又定製了Ruby代碼高亮功能:jquery

data:text/html, <style type="text/css">#e{position:absolute;top:0;right:0;bottom:0;left:0;}</style><div id="e"></div><script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script><script>var e=ace.edit("e");e.setTheme("ace/theme/monokai");e.getSession().setMode("ace/mode/ruby");</script>

 

效果以下圖:程序員

 

實際上,若是minikomi的代碼已經支持多種語言高亮,如Python,只須要把「markdown」換成「python」,效果以下:web

你覺得到此爲止了?中國開發者assassindesign以爲只是文本編輯器就太無聊了,又提供了塗鴉版本:ajax

data:text/html, <body><canvas id="dyDraw">你的瀏覽器不支持HTML5 Canvas</canvas></body><script>function $(id){return document.getElementById(id);} $('dyDraw').width=document.body.clientWidth;$('dyDraw').height=document.body.clientHeight;if(window.addEventListener){window.addEventListener('load',function(){var canvas,canvastext;var hua=false;function dyDrawing(){canvas=$('dyDraw');canvastext=canvas.getContext('2d');canvas.addEventListener('mousedown',canvasMouse,false);canvas.addEventListener('mousemove',canvasMouse,false);window.addEventListener('mouseup',canvasMouse,false);} function canvasMouse(dy){var x,y;if(dy.layerX||dy.layerX==0){x=dy.layerX;y=dy.layerY;}else if(dy.offsetX||dy.offsetX==0){x=dy.offsetX;y=dy.offsetY;} x-=dyDraw.offsetLeft;y-=dyDraw.offsetTop;if(dy.type=='mousedown'){hua=false;canvastext.beginPath();canvastext.moveTo(x,y);hua=true;}else if(dy.type=='mousemove'){if(hua){canvastext.strokeStyle="rgb(255,0,0)";canvastext.lineWidth=9;canvastext.lineTo(x,y);canvastext.stroke();}}else if(dy.type=='mouseup'){hua=false;}} dyDrawing();},false);}</script>

效果以下:json

極客的創意的無窮的,各位讀者,大家是否有更好的創意,歡迎跟帖!

相關文章
相關標籤/搜索