Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created image class #342

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/effects/texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
anchor = document.createElement('a');
}

var Texture = Two.Texture = function(src, callback) {
var Texture = Two.Texture = function(source, callback) {

this._renderer = {};
this._renderer.type = 'texture';
Expand All @@ -35,10 +35,10 @@
this.bind(Two.Events.load, loaded);
}

if (_.isString(src)) {
this.src = src;
} else if (_.isElement(src)) {
this.image = src;
if (_.isString(source)) {
this.src = source;
} else if (_.isElement(source)) {
this.image = source;
}

this._update();
Expand Down
113 changes: 113 additions & 0 deletions src/image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
(function(Two) {

// output example:
//
// <image
// preserveAspectRatio="none"
// href="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png"
// height="100"
// width="300"
// />

var _ = Two.Utils;

/**
* @class
* @description Class that renders an image as an Image tag, it does not use the SVG header.
* @param {number} [x=0] - x position at center
* @param {numbert} [y=0] - y position at center
* @param {number} [width] - width of image
* @param {number} [height] - height of image
* @param {string} [imageDataSource] - image data encoded as a base 64 string
* @param {boolean} [preserveAspectRatio] - boolean to preserve the aspect ratio
*/
var Image = Two.Image = function(x, y, width, height, imageDataSource, preserveAspectRatio) {

Two.Shape.call(this);

this._renderer.type = 'image';

this.width = width;
this.height = height;

this.href = imageDataSource;
this.opacity = 1.0;
this.className = '';
this.visible = true;

// this should better be in future an enum that can describe what all renders can do.
// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio
if (!_.isUndefined(preserveAspectRatio)) {
this.preserveAspectRatio = !!preserveAspectRatio;
}

if (_.isNumber(x) && _.isNumber(y)) {
this.translation.set(x, y);
}

this._update();

};

_.extend(Image, {

Properties: [
'width',
'height',
'href',
'preserveAspectRatio',
'opacity',
'className',
'visible'
],

MakeObservable: function(obj) {
Two.Shape.MakeObservable(obj);
_.each(Image.Properties, Two.Utils.defineProperty, obj);
}

});

_.extend(Image.prototype, Two.Shape.prototype, {

_flagWidth: false,
_flagHeight: false,
_flagHref: false,
_flagPreserveAspectRatio: false,
_flagVisible: false,
_flagClassName: false,
_flagOpacity: false,

_width: 0,
_height: 0,
_href: '',
_preserveAspectRatio: false,
_opacity: 1.0,
_className: '',
_visible: true,

constructor: Image,

_update: function() {

Two.Shape.prototype._update.call(this);
return this;

},

flagReset: function() {

this._flagWidth = this._flagHeight = this._flagHref
= this._flagPreserveAspectRatio = this._flagVisible
= this._flagClassName = this._flagOpacity = false;

Two.Shape.prototype.flagReset.call(this);
return this;

}

});

Image.MakeObservable(Image.prototype);

})((typeof global !== 'undefined' ? global : (this || window)).Two);
34 changes: 34 additions & 0 deletions src/renderer/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,40 @@

return this.flagReset();

},

image: function(ctx, forced) {

var matrix = this._matrix.elements;
var width = this._width;
var height = this._height;
var href = this._href;
var preserveAspectRatio = this._preserveAspectRatio;
var opacity = this._opacity;
var visible = this._visible;
var defaultMatrix = isDefaultMatrix(matrix);

if (!forced && (!visible)) {
return this;
}

if (!defaultMatrix) {
ctx.save();
ctx.transform(matrix[0], matrix[3], matrix[1], matrix[4], matrix[2], matrix[5]);
}

// TODO: Handle `preserveAspectRatio` intelligently...
var image = Two.Texture.getImage(href);

if (image) {
ctx.save();
ctx.translate(x, y);
ctx.drawImage(image, - width / 2, - height / 2, width, height);
ctx.restore();
}

return this.flagReset();

}

},
Expand Down
61 changes: 61 additions & 0 deletions src/renderer/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,69 @@

}

},

image: {

render: function(domElement) {

this._update();

var changed = {};

var flagMatrix = this._matrix.manual || this._flagMatrix;

if (flagMatrix) {
changed.transform = 'matrix(' + this._matrix.toString() + ')';
}

if (this._flagWidth) {
changed['width'] = this._width;
}

if (this._flagHeight) {
changed['height'] = this._height;
}

if (this._flagHref) {
changed['href'] = this._href;
}

if (this._flagPreserveAspectRatio) {
changed['preserveAspectRatio'] = this._preserveAspectRatio ? 'none' : 'meet';
}

if (this._flagOpacity) {
changed.opacity = this._opacity;
}

if (this._flagClassName) {
changed['class'] = this._className;
}

if (this._flagVisible) {
changed.visibility = this._visible ? 'visible' : 'hidden';
}

if (!this._renderer.elem) {

changed.id = this.id;

this._renderer.elem = svg.createElement('image', changed);
domElement.defs.appendChild(this._renderer.elem);

} else {

svg.setAttributes(this._renderer.elem, changed);

}
return this.flagReset();

}

}


};

/**
Expand Down
1 change: 1 addition & 0 deletions utils/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var files = [
path.resolve(__dirname, '../src/renderer/webgl.js'),
path.resolve(__dirname, '../src/shape.js'),
path.resolve(__dirname, '../src/path.js'),
path.resolve(__dirname, '../src/image.js'),
path.resolve(__dirname, '../src/shapes/line.js'),
path.resolve(__dirname, '../src/shapes/rectangle.js'),
path.resolve(__dirname, '../src/shapes/ellipse.js'),
Expand Down