`
wuzijingaip
  • 浏览: 335234 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

html5 canvas demo 好列子

阅读更多



<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[i]);
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[i];
        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>






  • 大小: 25.3 KB
  • 大小: 25.3 KB
分享到:
评论

相关推荐

    WPF创建基本工程Demo

    WPF提供了多种布局容器,如Canvas、WrapPanel、UniformGrid等,它们根据特定规则排列子元素。例如,WrapPanel会在一行内排列元素,当空间不足时换行。 **9. 控件模板与数据模板** 控制模板允许自定义控件的视觉外观...

    ProjectL:L的列子代码

    L的列子代码 目录: googledemo.customview 谷歌的demo projectl AppForegroundStateManager 检测Android应用的启动与关闭 CustomView 自定义View CustomTitleView 自定义标题的view CustomImageView 自定义...

    Silverlight布局

    提供的"WebCast20101102_Demo"文件应该包含示例代码和演示,建议配合学习,以便更好地理解和掌握Silverlight布局的使用。 总之,Silverlight的布局系统为开发者提供了丰富的选择和强大的灵活性,是创建美观且响应式...

    自定义View

    `ViewDemo`中可能包含一个自定义网格布局的示例,使得可以更灵活地排列子View。 4. 功能型自定义View:这类自定义View通常是为了实现特定的功能,比如计数器、日历等。它们可能需要额外的数据模型和业务逻辑。在`...

    Unity3d C#教程案例

    教程可能讲解如何使用Unity的UI组件(如Canvas、Text、Button等)来构建游戏菜单、提示信息、计分板等,并通过C#脚本来响应用户的输入事件。 七、物理系统与碰撞检测 Unity3D内置了强大的物理引擎,支持刚体动力学...

Global site tag (gtag.js) - Google Analytics