2014-10-02 69 views
0

我想创建一个动画应用程序演示:加载sceenshot1 - >按钮单击 - >切换到sceenshot2 - >输入2行文本。我看到一个网站做类似的动画(https://www.pixelapse.com/)。使用HTML5 canvas或CSS3动画演示应用程序快照

html5 canvas或CSS3是创建此动画的正确工具吗?我找到了这个库。 https://github.com/GwennaelBuchet/SceneGraph.js。有没有其他工具可以创建这种动画?

enter image description here

enter image description here

+0

所以你希望有一个评论气囊是附加注释的补充意见进来吗? – markE 2014-10-02 16:28:11

+0

是的,背景图片应该先加载 - >按钮点击 - >切换到下一个背景图片 - >显示2行文字。这是我想要实现的动画。 – angelokh 2014-10-02 16:56:29

+0

你现在的愿望更加清晰,谢谢!那么到目前为止你有什么代码? – markE 2014-10-02 17:03:42

回答

2

下面是一个例子和演示,让你开始:

一个评论:

enter image description here

多个注释:

enter image description here

  • 绘制与所述注释相关联的图像:context.drawImage

  • 画出注释的文本部分,并根据需要把它包装到多个行:见在下面的例子中的wrapText功能。

  • 在图像周围绘制圆角矩形+注释:请参阅下面示例中的roundedRect函数。

示例代码和演示,让您开始:

var canvas = document.getElementById("canvas"); 
 
var ctx = canvas.getContext("2d"); 
 
var cw = canvas.width; 
 
var ch = canvas.height; 
 

 
var x = 25; 
 
var y = 25; 
 
var width = 350; 
 
var height = 0; 
 
var radius = 10; 
 
var padding = 10; 
 

 
var comments = []; 
 
var commentDisplayCount = 1; 
 
comments.push({ 
 
    imgIndex: 0, 
 
    name: 'jennifer', 
 
    comment: 'Lacking contrast in the colors around this blue thing on top of the swan origami. What do you think we can do to punch it up a bit?' 
 
}); 
 
comments.push({ 
 
    imgIndex: 1, 
 
    name: 'Richard', 
 
    comment: 'Maybe we try a darker shade of blue in the sides of that blue thing?' 
 
}); 
 
comments.push({ 
 
    imgIndex: 0, 
 
    name: 'jennifer', 
 
    comment: 'Blah, Blah, Blah' 
 
}); 
 
comments.push({ 
 
    imgIndex: 1, 
 
    name: 'Richard', 
 
    comment: 'Yep, Yep, Yep' 
 
}); 
 

 
// preload images 
 
// put the paths to your images in imageURLs[] 
 
var imageURLs = []; 
 
// push all your image urls! 
 
imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/multple/face1.png"); 
 
imageURLs.push("https://dl.dropboxusercontent.com/u/139992952/multple/face2.png"); 
 

 
// the loaded images will be placed in imgs[] 
 
var imgs = []; 
 
var imagesOK = 0; 
 
loadAllImages(start); 
 

 
function loadAllImages(callback) { 
 
    for (var i = 0; i < imageURLs.length; i++) { 
 
    var img = new Image(); 
 
    imgs.push(img); 
 
    img.onload = function() { 
 
     imagesOK++; 
 
     if (imagesOK >= imageURLs.length) { 
 
     callback(); 
 
     } 
 
    }; 
 
    img.onerror = function() { 
 
     alert("image load failed"); 
 
    } 
 
    img.crossOrigin = "anonymous"; 
 
    img.src = imageURLs[i]; 
 
    } 
 
} 
 

 
function start() { 
 

 
    // the imgs[] array now holds fully loaded images 
 
    // the imgs[] are in the same order as imageURLs[] 
 

 
    draw(); 
 
} 
 

 

 
function draw() { 
 
    var accumHeight = 0; 
 
    var imageWidth = 35; 
 

 
    ctx.clearRect(0, 0, cw, ch); 
 
    for (var i = 0; i < commentDisplayCount; i++) { 
 

 
    ctx.drawImage(imgs[comments[i].imgIndex], x + padding, y + accumHeight + padding, 25, 25); 
 

 
    accumHeight = wrapText(
 
     comments[i].comment, 
 
     x + padding + imageWidth, 
 
     y + padding + accumHeight, 
 
     width - padding * 2 - imageWidth, 12, "verdana" 
 
    ); 
 

 
    } 
 
    roundedRect(x, y, width, accumHeight, radius); 
 
} 
 

 

 
function roundedRect(x, y, w, h, r) { 
 
    ctx.beginPath(); 
 
    ctx.moveTo(x + r, y); 
 
    ctx.lineTo(x + w - r, y); 
 
    ctx.quadraticCurveTo(x + w, y, x + w, y + r); 
 
    ctx.lineTo(x + w, y + h - r); 
 
    ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h); 
 
    ctx.lineTo(x + r, y + h); 
 
    ctx.quadraticCurveTo(x, y + h, x, y + h - r); 
 
    ctx.lineTo(x, y + r); 
 
    ctx.quadraticCurveTo(x, y, x + r, y); 
 
    ctx.closePath(); 
 
    ctx.strokeStyle = 'black'; 
 
    ctx.fillStyle = 'white'; 
 
    ctx.stroke(); 
 
    ctx.globalCompositeOperation = 'destination-over'; 
 
    ctx.fill(); 
 
    ctx.globalCompositeOperation = 'source-over'; 
 
} 
 

 

 
function wrapText(text, x, y, maxWidth, fontSize, fontFace) { 
 
    var words = text.split(' '); 
 
    var line = ''; 
 
    var lineHeight = fontSize; 
 
    y += fontSize; 
 
    ctx.font = fontSize + " " + fontFace; 
 
    ctx.fillStyle = 'black'; 
 
    for (var n = 0; n < words.length; n++) { 
 
    var testLine = line + words[n] + ' '; 
 
    var metrics = ctx.measureText(testLine); 
 
    var testWidth = metrics.width; 
 
    if (testWidth > maxWidth) { 
 
     ctx.fillText(line, x, y); 
 
     line = words[n] + ' '; 
 
     y += lineHeight; 
 
    } else { 
 
     line = testLine; 
 
    } 
 
    } 
 
    ctx.fillText(line, x, y); 
 
    return (y); 
 
} 
 

 

 

 
$("#test").click(function() { 
 
    if (++commentDisplayCount > comments.length) { 
 
    commentDisplayCount = comments.length; 
 
    } 
 
    draw(); 
 
});
body{ background-color: ivory; } 
 
canvas{border:1px solid red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<button id="test">Add a Comment</button> 
 
<br> 
 
<canvas id="canvas" width=400 height=400></canvas>