- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试执行以下操作:
到目前为止我的代码
function init() {
//find canvas and load images, wait for last image to load
var canvas = document.getElementById("Canvas");
var stage = new createjs.Stage(canvas);
// set width and height
stage.canvas.width = 500;
stage.canvas.height = 500;
var number = 5;
for(var i = 0; i < number; i++){
var shape = new createjs.Shape();
var shapeWidth = Math.floor(Math.random() * 100);
var shapeHeight = 50;
shape.graphics.beginFill('red').drawRect(0, 0, shapeWidth, shapeHeight);
shape.x = i * 51;
shape.y = canvas.height - 50;
stage.addChild(shape);
stage.update();
}
}
JSFiddle:https://jsfiddle.net/n5kgbe3g/1/
提前致谢
最佳答案
如果您包含约束(例如划分的最小和最大大小),则随机划分长度可能会变得非常复杂,必须保持真正的随机性(不能作弊,如果划分不完全适合,只需添加最后一位) .
所以简单的方法是设置一个简单的约束。将长度分为n个随机长度,每个长度不小于x。
// length is the length to divide randomly
// count is the number of divisions
// min is the minimum division size
// array (optional) the array to add the data too. Will be created if not given
// returns array containing count items. Each item is a random value > min
// where the total sum of all the items is === length.
// If the array already has items then new items are added. Only the new
// items will satisfy the length constraint.
// NOTE There is no vetting YOU MUST SUPPLY good arguments or the function
// will return nonsense.
function randomDiv(length,count,min,array = []){
var rand, total = 0; // total to normalise random values
length -= count * min; // get the remaining length after removing length
// used by min length
var startAt = array.length; // If adding to array get the start index
// get a set of random values and sum them
while(array.length - startAt < count){
ran = Math.random();
total += ran;
array.push(ran);
}
// use the total sum of random values to
// normalise the array items to be the correct length
while(count --){
array[startAt + count] = min + array[startAt + count] / total * length;
}
return array;
}
使用
// get 20 box widths to fit canvas width no smaller than 10 pixels
var boxes = randomDiv(canvas.width, 20, 10);
// to reuse the array. Create a referance
var boxes = [];
// each time out call the function pass the referance and empty it or
// result is added
boxes.length = 0;
boxes = randomDiv(canvas.width, 20, 10, boxes);
创建一组具有不同品质的盒子
// 100 small lengths for half the canvas one or more pixels wide
var boxes = randomDiv(canvas.width / 2, 100, 1);
// get 10 more boxes for the remaining half larger than 30 pixels
boxes = randomDiv(canvas.width / 2, 10, 30, boxes);
// shuffle the array
var shuffled = [];
while(boxes.length > 0){
shuffled.push(boxes.splice( Math.floor(Math.random() * boxes.length) , 1)[0]);
}
关于javascript - 生成许多随机宽度和高度 block 并将它们填充到 HTML5 Canvas 的整个宽度 - EaselJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43513367/
我正在尝试水平翻转 SpriteSheet 动画中的 Sprite 。我从this example开始 我在创建SpriteSheet后立即添加了翻转的帧 var ss = new creat
我正在尝试水平翻转 SpriteSheet 动画中的 Sprite 。我从 this example 开始 我在创建 SpriteSheet 后立即添加了翻转帧 var ss = new cr
是否可以使透明rect可点击? 我不想填充它,所以看起来像 shape.graphics.setStrokeStyle(2).beginStroke("#000").rect(0, 0, 1
如何在createJS中绘制圆弧。我已经经历过 arcTo 函数,但这不是我想要的。我希望能够绘制几条弧线,这些弧线放在一起时类似于一个圆。例如:我希望能够使用 8 条弧线绘制一个圆。无法使用 Arc
当我尝试使用时: temp = new createjs.Bitmap(obj.path) 我收到错误: Uncaught An error has occurred. This is most li
我正在用 EaselJS 制作一个旋转矩形,但它不像我想的那样工作。 我认为,如果我想制作一个围绕其中心在 x=100, y=100 位置旋转的正方形 (40x40),我需要将它的注册点设置为 reg
我在使用 EaselJS 时遇到了困难。基本上我想创建一个简单的网格并突出显示所选的实际元素: var stageWidth = 800, stageHeight = 600, cell_
我最近发现了EaselJS,我正在尝试制作一个非常简单的 Canvas 射击游戏。 我的 Canvas 宽度为 500 像素,高度为 500 像素。 当用户的鼠标进入 Canvas 时,目标会随着鼠标
我正在使用 EaselJS SpriteSheets,我想知道如何让我的英雄停止平稳运行,因此,如果 SpriteSheet 正在播放“运行”动画并且位于第 5 帧,我需要通过帧进行动画处理6, 7,
我的形状要么从地板上掉下来,要么消失。我的代码很简单,如果您复制并粘贴,您就可以看到所有内容。无论如何,听到的是代码。我想我可能没有在tick函数中的碰撞中添加一些东西。 var box, gravi
EaselJS.js 库有很多我的项目中不需要的函数(我不调用它们)。有没有办法检测这些功能并为每个项目删除它们?我需要尽可能减小文件大小。 附注我的easeljs.js是在Adobe Flash C
我正在尝试提高 Canvas 上的性能,因为每个 stage.update() 上都会重新绘制所有内容(多个区域),这就是它的工作原理。我只想绘制/添加一个区域。我可以让它工作,但是由于更新,所有以前
我正在使用 EaselJS 用 JS 编写一个简单的游戏。我尝试保持一切面向对象,以保持游戏状态、EaselJS 对象状态以及 Canvas 上显示的内容同步。我希望能够通过更改属性来更改 Canva
我希望我的位图转到单击位置,但我希望看到 bitmap.x/bitmap.y 和 click.x/click.y 之间的进展我怎样才能制作这个动画? 非常感谢 最佳答案 单击舞台时,您可以使用 Twe
我需要使用 Shape 对象创建可拖动的随机矩形。此函数在单击鼠标的位置创建新的矩形。 var rects = []; var i=0; var stage; window.onload = func
在代码的开头,我设置了一个刻度事件。以下 3 行有效。 createjs.Ticker.addEventListener("tick", handleTick); createjs.Ticker.us
我正在尝试在 Canvas 中创建可拖动的背景图像。目前我有这样的东西: var stage = new createjs.Stage("canvas"); createjs.Ticker.addEv
所以我正在使用 easeljs,并且我有一堆要回收的图像。由于某种原因,每当我尝试将它们添加到舞台 stage.update() 就不再起作用。 (我使用警报来测试这一点,因此它可能会变得非常慢例如加
我刚开始使用 easeljs 库。我创建了一个带舞台的 Canvas ,它可以正常工作。 我想添加位图作为按钮并将它们并排添加到舞台底部。 我试过以下方法,但它会将位图放在彼此的顶部。 var but
我刚开始通过在线教程学习 javascript,并从 GitHub 存储库下载文件。文件类型为“easeljs-0.7.1.min”。当我输入下面给出的代码时,它没有显示任何内容在浏览器上。是我下载的
我是一名优秀的程序员,十分优秀!