mirror of
https://github.com/konvajs/konva.git
synced 2026-03-03 16:58:33 +08:00
updated pixelate unit tests
This commit is contained in:
@@ -2,36 +2,26 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Pixelate Filter. Averages groups of pixels and redraws
|
* Pixelate Filter. Averages groups of pixels and redraws
|
||||||
* them as larger "pixels".
|
* them as larger pixels
|
||||||
* Performs w*h pixel reads and w*h pixel writes.
|
|
||||||
* @function
|
* @function
|
||||||
* @author ippo615
|
* @author ippo615
|
||||||
* @memberof Kinetic.Filters
|
* @memberof Kinetic.Filters
|
||||||
* @param {ImageData} src, the source image data (what will be transformed)
|
* @param {Object} imageData
|
||||||
* @param {ImageData} dst, the destination image data (where it will be saved)
|
|
||||||
* @param {Object} opt
|
|
||||||
* @param {Number} [opt.pixelWidth], The width (in pixels) of the
|
|
||||||
* new larger pixels, default is 8.
|
|
||||||
* @param {Number} [opt.pixelHeight], The height (in pixels) of the
|
|
||||||
* new larger pixels, default is 8.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Pixelate = function (src, dst, opt) {
|
Kinetic.Filters.Pixelate = function (imageData) {
|
||||||
|
|
||||||
var xBinSize = Math.ceil(opt.pixelWidth) || 8,
|
var pixelSize = Math.ceil(this.pixelSize()),
|
||||||
yBinSize = Math.ceil(opt.pixelHeight) || 8;
|
width = imageData.width,
|
||||||
|
height = imageData.height,
|
||||||
var xSize = src.width,
|
imageData = imageData.data,
|
||||||
ySize = src.height,
|
x, y, i,
|
||||||
srcPixels = src.data,
|
pixelsPerBin = pixelSize * pixelSize,
|
||||||
dstPixels = dst.data,
|
red, green, blue, alpha,
|
||||||
x, y, i;
|
nBinsX = Math.ceil(width / pixelSize),
|
||||||
var pixelsPerBin = xBinSize * yBinSize,
|
nBinsY = Math.ceil(height / pixelSize),
|
||||||
red, green, blue, alpha,
|
xBinStart, xBinEnd, yBinStart, yBinEnd,
|
||||||
nBinsX = Math.ceil(xSize / xBinSize),
|
xBin, yBin, pixelsInBin;
|
||||||
nBinsY = Math.ceil(ySize / yBinSize),
|
|
||||||
xBinStart, xBinEnd, yBinStart, yBinEnd,
|
|
||||||
xBin, yBin, pixelsInBin;
|
|
||||||
|
|
||||||
for (xBin = 0; xBin < nBinsX; xBin += 1) {
|
for (xBin = 0; xBin < nBinsX; xBin += 1) {
|
||||||
for (yBin = 0; yBin < nBinsY; yBin += 1) {
|
for (yBin = 0; yBin < nBinsY; yBin += 1) {
|
||||||
@@ -43,22 +33,22 @@
|
|||||||
alpha = 0;
|
alpha = 0;
|
||||||
|
|
||||||
// Determine which pixels are included in this bin
|
// Determine which pixels are included in this bin
|
||||||
xBinStart = xBin * xBinSize;
|
xBinStart = xBin * pixelSize;
|
||||||
xBinEnd = xBinStart + xBinSize;
|
xBinEnd = xBinStart + pixelSize;
|
||||||
yBinStart = yBin * yBinSize;
|
yBinStart = yBin * pixelSize;
|
||||||
yBinEnd = yBinStart + yBinSize;
|
yBinEnd = yBinStart + pixelSize;
|
||||||
|
|
||||||
// Add all of the pixels to this bin!
|
// Add all of the pixels to this bin!
|
||||||
pixelsInBin = 0;
|
pixelsInBin = 0;
|
||||||
for (x = xBinStart; x < xBinEnd; x += 1) {
|
for (x = xBinStart; x < xBinEnd; x += 1) {
|
||||||
if( x >= xSize ){ continue; }
|
if( x >= width ){ continue; }
|
||||||
for (y = yBinStart; y < yBinEnd; y += 1) {
|
for (y = yBinStart; y < yBinEnd; y += 1) {
|
||||||
if( y >= ySize ){ continue; }
|
if( y >= height ){ continue; }
|
||||||
i = (xSize * y + x) * 4;
|
i = (width * y + x) * 4;
|
||||||
red += srcPixels[i + 0];
|
red += imageData[i + 0];
|
||||||
green += srcPixels[i + 1];
|
green += imageData[i + 1];
|
||||||
blue += srcPixels[i + 2];
|
blue += imageData[i + 2];
|
||||||
alpha += srcPixels[i + 3];
|
alpha += imageData[i + 3];
|
||||||
pixelsInBin += 1;
|
pixelsInBin += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -71,14 +61,14 @@
|
|||||||
|
|
||||||
// Draw this bin
|
// Draw this bin
|
||||||
for (x = xBinStart; x < xBinEnd; x += 1) {
|
for (x = xBinStart; x < xBinEnd; x += 1) {
|
||||||
if( x >= xSize ){ continue; }
|
if( x >= width ){ continue; }
|
||||||
for (y = yBinStart; y < yBinEnd; y += 1) {
|
for (y = yBinStart; y < yBinEnd; y += 1) {
|
||||||
if( y >= ySize ){ continue; }
|
if( y >= height ){ continue; }
|
||||||
i = (xSize * y + x) * 4;
|
i = (width * y + x) * 4;
|
||||||
dstPixels[i + 0] = red;
|
imageData[i + 0] = red;
|
||||||
dstPixels[i + 1] = green;
|
imageData[i + 1] = green;
|
||||||
dstPixels[i + 2] = blue;
|
imageData[i + 2] = blue;
|
||||||
dstPixels[i + 3] = alpha;
|
imageData[i + 3] = alpha;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,48 +76,14 @@
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Kinetic.Factory.addFilterGetterSetter(Kinetic.Node, 'pixelationSize', 8);
|
Kinetic.Factory.addFilterGetterSetter(Kinetic.Node, 'pixelSize', 8);
|
||||||
|
|
||||||
Kinetic.Filters.Pixelate = function(src,dst,opt){
|
/**
|
||||||
if( this === Kinetic.Filters ){
|
* get/set pixel size
|
||||||
Pixelate(src, dst||src, opt );
|
* @name pixelSize
|
||||||
}else{
|
* @method
|
||||||
Pixelate.call(this, src, dst||src, opt || {
|
* @memberof Kinetic.Node.prototype
|
||||||
pixelWidth: this.pixelationSize(),
|
* @param {Integer} pixelSize
|
||||||
pixelHeight: this.pixelationSize()
|
* @returns {Integer}
|
||||||
});
|
*/
|
||||||
}
|
})();
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get filter pixel width. Returns the width of a pixelated pixel. Must be
|
|
||||||
* an integer greater than 0. A value of 4 means a pixel in the filtered
|
|
||||||
* image is as wide as 4 pixels in the original.
|
|
||||||
* @name getPixelWidth
|
|
||||||
* @method
|
|
||||||
* @memberof Kinetic.Image.prototype
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* set filter pixel width.
|
|
||||||
* @name setPixelWidth
|
|
||||||
* @method
|
|
||||||
* @memberof Kinetic.Image.prototype
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get filter pixel height. Returns the height of a pixelated pixel. Must be
|
|
||||||
* an integer greater than 0. A value of 4 means a pixel in the filtered
|
|
||||||
* image is as tall as 4 pixels in the original.
|
|
||||||
* @name getPixelHeight
|
|
||||||
* @method
|
|
||||||
* @memberof Kinetic.Image.prototype
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* set filter pixel height.
|
|
||||||
* @name setPixelHeight
|
|
||||||
* @method
|
|
||||||
* @memberof Kinetic.Image.prototype
|
|
||||||
*/
|
|
||||||
})();
|
|
||||||
@@ -89,9 +89,9 @@
|
|||||||
<!--<script src="unit/filters/ConvolvePack-test.js"></script>-->
|
<!--<script src="unit/filters/ConvolvePack-test.js"></script>-->
|
||||||
<script src="unit/filters/Grayscale-test.js"></script>
|
<script src="unit/filters/Grayscale-test.js"></script>
|
||||||
<script src="unit/filters/Enhance-test.js"></script>
|
<script src="unit/filters/Enhance-test.js"></script>
|
||||||
<!--<script src="unit/filters/Polar-test.js"></script>
|
<!--<script src="unit/filters/Polar-test.js"></script>-->
|
||||||
<script src="unit/filters/Pixelate-test.js"></script>
|
<script src="unit/filters/Pixelate-test.js"></script>
|
||||||
<script src="unit/filters/Noise-test.js"></script>
|
<!--<script src="unit/filters/Noise-test.js"></script>
|
||||||
<script src="unit/filters/Threshold-test.js"></script>
|
<script src="unit/filters/Threshold-test.js"></script>
|
||||||
<script src="unit/filters/Levels-test.js"></script>
|
<script src="unit/filters/Levels-test.js"></script>
|
||||||
<script src="unit/filters/Flip-test.js"></script>
|
<script src="unit/filters/Flip-test.js"></script>
|
||||||
|
|||||||
@@ -1,69 +1,4 @@
|
|||||||
suite('Pixelate', function () {
|
suite('Pixelate', function () {
|
||||||
// ======================================================
|
|
||||||
test('pixelate', function (done) {
|
|
||||||
var stage = addStage();
|
|
||||||
|
|
||||||
var shapesLayer = new Kinetic.Layer();
|
|
||||||
|
|
||||||
// The important line!
|
|
||||||
shapesLayer.on('draw', function () {
|
|
||||||
var imageData = this.getContext().getImageData(0, 0, this.getCanvas().width/2, this.getCanvas().height);
|
|
||||||
var scratchData = this.getContext().createImageData(imageData); // only size copied
|
|
||||||
Kinetic.Filters.Pixelate(imageData,scratchData,{pixelWidth:8,pixelHeight:16});
|
|
||||||
Kinetic.Filters.ColorStretch(scratchData,imageData,{});
|
|
||||||
this.getContext().putImageData(imageData,0,0);
|
|
||||||
});
|
|
||||||
|
|
||||||
var triangle = new Kinetic.RegularPolygon({
|
|
||||||
x: stage.getWidth() / 4,
|
|
||||||
y: stage.getHeight() / 2,
|
|
||||||
sides: 3,
|
|
||||||
radius: 80,
|
|
||||||
fillRadialGradientStartPoint: 0,
|
|
||||||
fillRadialGradientStartRadius: 0,
|
|
||||||
fillRadialGradientEndPoint: 0,
|
|
||||||
fillRadialGradientEndRadius: 70,
|
|
||||||
fillRadialGradientColorStops: [0, '#881111', 0.5, '#888811', 1, '#000088'],
|
|
||||||
stroke: 'black',
|
|
||||||
strokeWidth: 4,
|
|
||||||
draggable: true
|
|
||||||
});
|
|
||||||
|
|
||||||
var circle = new Kinetic.Circle({
|
|
||||||
x: 3 * stage.getWidth() / 4,
|
|
||||||
y: stage.getHeight() / 2,
|
|
||||||
radius: 70,
|
|
||||||
fill: '#880000',
|
|
||||||
stroke: 'black',
|
|
||||||
strokeWidth: 4,
|
|
||||||
draggable: true,
|
|
||||||
id: 'myCircle'
|
|
||||||
});
|
|
||||||
|
|
||||||
for( var i=0; i<10; i+=1 ){
|
|
||||||
for( var j=0; j<10; j+=1 ){
|
|
||||||
var rect = new Kinetic.Rect({
|
|
||||||
x: i/10*stage.getWidth(),
|
|
||||||
y: j/10*stage.getHeight(),
|
|
||||||
width: stage.getWidth()/10,
|
|
||||||
height: stage.getHeight()/10,
|
|
||||||
fill: (i+j)%2===0?'#FF0000':'#FFFF00',
|
|
||||||
stroke: 'black',
|
|
||||||
strokeWidth: 4,
|
|
||||||
draggable: true
|
|
||||||
});
|
|
||||||
shapesLayer.add(rect);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
shapesLayer.add(circle);
|
|
||||||
shapesLayer.add(triangle);
|
|
||||||
|
|
||||||
stage.add(shapesLayer);
|
|
||||||
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
test('tween pixelate', function(done) {
|
test('tween pixelate', function(done) {
|
||||||
@@ -73,34 +8,33 @@ suite('Pixelate', function () {
|
|||||||
imageObj.onload = function() {
|
imageObj.onload = function() {
|
||||||
|
|
||||||
var layer = new Kinetic.Layer();
|
var layer = new Kinetic.Layer();
|
||||||
darth = new Kinetic.Image({
|
lion = new Kinetic.Image({
|
||||||
x: 10,
|
x: 10,
|
||||||
y: 10,
|
y: 10,
|
||||||
image: imageObj,
|
image: imageObj,
|
||||||
draggable: true
|
draggable: true
|
||||||
});
|
});
|
||||||
|
|
||||||
layer.add(darth);
|
layer.add(lion);
|
||||||
stage.add(layer);
|
stage.add(layer);
|
||||||
|
|
||||||
darth.setFilter(Kinetic.Filters.Pixelate);
|
lion.cache();
|
||||||
darth.setPixelWidth(16);
|
lion.filters([Kinetic.Filters.Pixelate]);
|
||||||
darth.setPixelHeight(16);
|
lion.pixelSize(16);
|
||||||
layer.draw();
|
layer.draw();
|
||||||
|
|
||||||
var tween = new Kinetic.Tween({
|
var tween = new Kinetic.Tween({
|
||||||
node: darth,
|
node: lion,
|
||||||
duration: 3.0,
|
duration: 3.0,
|
||||||
pixelWidth: 1,
|
pixelSize: 1,
|
||||||
pixelHeight: 1,
|
|
||||||
easing: Kinetic.Easings.EaseInOut
|
easing: Kinetic.Easings.EaseInOut
|
||||||
});
|
});
|
||||||
|
|
||||||
darth.on('mouseover', function() {
|
lion.on('mouseover', function() {
|
||||||
tween.play();
|
tween.play();
|
||||||
});
|
});
|
||||||
|
|
||||||
darth.on('mouseout', function() {
|
lion.on('mouseout', function() {
|
||||||
tween.reverse();
|
tween.reverse();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
suite('Polar Coordinates', function () {
|
suite('Polar', function () {
|
||||||
// ======================================================
|
// ======================================================
|
||||||
test('to polar', function (done) {
|
test('to polar', function (done) {
|
||||||
var stage = addStage();
|
var stage = addStage();
|
||||||
|
|||||||
Reference in New Issue
Block a user