六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 263|回复: 0

html5 canvas demo 好列子

[复制链接]

升级  24.67%

76

主题

76

主题

76

主题

举人

Rank: 3Rank: 3

积分
274
 楼主| 发表于 2013-1-29 13:55:01 | 显示全部楼层 |阅读模式
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>TextCanvas Demo</title>


<script>
var video = document.querySelector('video');
var togglePlay = document.querySelector('#play');
var position = document.querySelector('#position');
var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');

addEvent(togglePlay, 'click', function () {
  video.playbackRate = 0.5;
  if (video.paused) {
    if (video.ended) video.currentTime = 0;
    video.play();
    this.value = "pause";
  } else {
    video.pause();
    this.value = "play";
  }
});

addEvent(video, 'timeupdate', function () {
  position.innerHTML = asTime(this.currentTime);

  // ctx.restore();
  ctx.drawImage(video, 0, 0, video.width, video.height, 0, 0, canvas.width, canvas.height);
});

addEvent(video, 'ended', function () {
  togglePlay.value = "play";
});

addEvent(video, 'canplay', function () {
  video.muted = true;
  document.querySelector('#duration').innerHTML = asTime(this.duration);
  startCanvas();
});


function startCanvas() {
  canvas.setAttribute('height', Math.floor(video.height));
  canvas.setAttribute('width', Math.floor(video.width));
  
  ctx.translate(canvas.width/2, canvas.height/2);
  ctx.scale(-1, 1);
  ctx.translate(-canvas.width/2, -canvas.height/2);
  ctx.drawImage(video, 0, 0, video.width, video.height, 0, 0, canvas.width, canvas.height);

  // var mirror = canvas.height * 0.6;
  // var grad = ctx.createLinearGradient(0, 0, 0, mirror);
  // grad.addColorStop(0, 'rgba(0, 0, 0, 0.5)');
  // grad.addColorStop(1, 'rgba(0, 0, 0, 1)');
  // ctx.fillStyle = grad;
  // ctx.rect(0, 0, canvas.width, mirror);
  // ctx.fill();
  // ctx.save();  
}

function asTime(t) {
  t = Math.round(t);
  var s = t % 60;
  var m = Math.round(t / 60);
  
  return two(m) + ':' + two(s);
}

function two(s) {
  s += "";
  if (s.length < 2) s = "0" + s;
  return s;
}
</script>

   <script type="text/javascript">
  

function TextCanvas(container) {
    this.container = container;
if (!container.style.position)
container.style.position = 'relative';
    var canvas = document.createElement('canvas');
    this.canvas = canvas;
canvas.style.position = 'absolute';
    container.appendChild(canvas);
    this.labels = [];
}

// Font and text properties.  These are applied to strings that are
// rendered with drawString.
TextCanvas.CSSStringProperties = 'color direction fontFamily fontSize fontSizeAdjust fontStretch fontStyle fontVariant fontWeight letterSpacing lineHeight textAlign textDecoration textIndent textShadow textTransform unicodeBidi whiteSpace wordSpacing'.split(' ');

TextCanvas.prototype.getContext = function(contextID) {
   var ctx = this.canvas.getContext(contextID);
if (contextID == '2d')
this.attachMethods(ctx, this);
return ctx;
};

TextCanvas.prototype.setDimensions = function(width, height) {
var container = this.container;
var canvas = this.canvas;
    // "canvas.width = width" doesn't work in Safari
    canvas.setAttribute('width', width);
    canvas.setAttribute('height', height);
this.container.style.width = width;
this.container.style.height = height;
}

TextCanvas.prototype.clear = function() {
    var canvas = this.canvas;
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < this.labels.length; i++)
this.container.removeChild(this.labels);
this.labels = [];
};

TextCanvas.prototype.attachMethods = function(ctx, controller) {
ctx.drawString = function(x, y, string) {
controller.addLabel(x, y, string);
};

ctx.clear = function () {
controller.clear();
};
   
    ctx.stringStyle = controller.container.style;
};

TextCanvas.prototype.addLabel = function(x, y, string) {
var label = document.createElement('div');
    label.innerHTML = string;
    var style = this.container.style;
    var cssNames = TextCanvas.CSSStringProperties;
    for (var i = 0; i < cssNames.length; i++) {
        var name = cssNames;
        label.style[name] = style[name];
    }
label.style.position = 'absolute';
label.style.left = x;
label.style.top = y;
this.container.appendChild(label);
this.labels.push(label);
}
   </script>
</head>
<body>
<video height="360" width="480">
<source src="file:::///C:/media/video/WMV_720P_Amazon.wmv" />
<source src="dizzy.ogv" />
</video>
<p>
<input type="button" id="play" value="play">
<span id="position">00:00</span> / <span id="duration"></span>
</p>
<canvas id="cc"></canvas>
<table>
<tr>
<td>
rfgtyhuyughghb
</td>
</tr>
</table>
   <div id="canvas-container" width=100 height=100>
   </div>
   
<table>
<tr>
<td>
rfgtyhuyughghb
</td>
</tr>
<tr>
<td>
rfgtyhuyughghb
</td>
</tr>
<tr>
<td>
rfgtyhuyughghb
</td>
</tr>
<tr>
<td>
rfgtyhuyughghb
</td>
</tr>
<tr>
<td>
rfgtyhuyughghb
</td>
</tr>
<tr>
<td>
rfgtyhuyughghb
</td>
</tr>
<tr>
<td>
rfgtyhuyughghb
</td>
</tr>
</table>
<video autoplay="true" controls="true" src="http://videos.mozilla.org/firefox/3.5/meet/meet.ogv" width="640" height="360">您的浏览器不支持 video 标签。</video>
   <script type="text/javascript">

var textshow = 'umNumNumNumNumNumNumNumNumNumNumNumNumNumNumNumNumNumNumNumNumNumNumNumNumNumNum';
     var controller = new TextCanvas(document.getElementById('canvas-container'));
     controller.setDimensions(400, 300);
     var ctx = controller.getContext('2d');
     var midx = 300/2, midy = 100/2;
     var tttt = 0,aaa=0;
     function redraw(ctx, t) {
/*for (i=0;i<4;i++){
  for(j=0;j<3;j++){
    ctx.beginPath();
    var x              = 25+j*50;               // x coordinate
    var y              = 25+i*50;               // y coordinate
    var radius         = 20;                    // Arc radius
    var startAngle     = 0;                     // Starting point on circle
    var endAngle       = Math.PI+(Math.PI*j)/2; // End point on circle
    var anticlockwise  = i%2==0 ? false : true; // clockwise or anticlockwise

    ctx.arc(x,y,radius,startAngle,endAngle, anticlockwise);

    if (i>1){
      ctx.fill();
    } else {
      ctx.stroke();
    }
  }
}

quadraticCurveTo(cp1x, cp1y, x, y) // BROKEN in Firefox 1.5 (see work around below)
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)


// Quadratric curves example
ctx.beginPath();
ctx.moveTo(75,25);
ctx.quadraticCurveTo(25,25,25,62.5);
ctx.quadraticCurveTo(25,100,50,100);
ctx.quadraticCurveTo(50,120,30,125);
ctx.quadraticCurveTo(60,120,65,100);
ctx.quadraticCurveTo(125,100,125,62.5);
ctx.quadraticCurveTo(125,25,75,25);
ctx.stroke();
*/
// Bezier curves example
ctx.beginPath();
ctx.moveTo(75,40);
ctx.bezierCurveTo(75,37,70,25,50,25);
ctx.bezierCurveTo(20,25,20,62.5,20,62.5);
ctx.bezierCurveTo(20,80,40,102,75,120);
ctx.bezierCurveTo(110,102,130,80,130,62.5);
ctx.bezierCurveTo(130,62.5,130,25,100,25);
ctx.bezierCurveTo(85,25,75,37,75,40);
ctx.fill();

   tttt++;
   aaa++;
   tttt = ( (tttt % (textshow.length * 10 )));

       var u = 2*t;
       var v = 3*t;
       var p0 = {x: midx + midx*Math.cos(u), y: midy + midy*Math.sin(u)};
       var p1 = {x: midx + midx*Math.cos(v), y: midy + midy*Math.sin(v)};
       ctx.clear();
       ctx.moveTo(p0.x, p0.y);
       ctx.lineTo(p1.x, p1.y);
     /*  ctx.moveTo(p0.x, p0.y);
       ctx.lineTo(p0.x + 8, p1.y % p0.y +;
       ctx.moveTo(p0.x, p0.y);
       ctx.lineTo(p1.x % p0.x - 8, p1.y % p0.y +;*/
       ctx.stroke();
       ctx.stringStyle.color = 'red';
       ctx.stringStyle.fontSize = 12;
       ctx.drawString(p0.x, p0.y, 'A');
       ctx.stringStyle.color = 'blue';
       ctx.stringStyle.fontSize = 18;
       ctx.drawString(p1.x, p1.y, 'B');
       ctx.stringStyle.color = 'green';
       ctx.stringStyle.fontSize = 60;
   ctx.drawString(800 - tttt ,100, u+'a'+textshow.length+'--'+textshow +tttt);
       ctx.stringStyle.color = '#a33aac';
       ctx.stringStyle.fontSize = 50;
   ctx.drawString(p1.x +aaa % 800, p1.y+aaa % 800-300,'abcdefg');
       ctx.stringStyle.color = '#aacabc';
       ctx.stringStyle.fontSize = 50;
   ctx.drawString(800-(p1.x +aaa % 800), p1.y+aaa % 800-300,'2134654');
     }
     
     // don't mistake me for a real animation system!
     var animation = {timer: null, value: 0, duration: 5 * 1000};
     
     function stepAnimation() {
       var t = (new Date().getTime() - animation.startTime) / animation.duration;
       animation.value = t %= 1.0;
       redraw(ctx, t * 2* Math.PI);
     }
     
     function startAnimation() {
       var framerate = 30;
       animation.startTime = new Date().getTime() - animation.value * animation.duration;
       animation.timer = animation.timer || setInterval(stepAnimation, 1000/framerate);
     }

     startAnimation();
   </script>
   
</body>
</html>



您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表