Skip to content

Commit

Permalink
First round, works great in safari
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanflorence committed Nov 21, 2010
1 parent 87d1eb4 commit b24e005
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
66 changes: 66 additions & 0 deletions Source/Transform.js
@@ -0,0 +1,66 @@
/*
---
name: Transform.js
license: MIT-style license.
authors: Ryan Florence <http://ryanflorence.com>
provides: [Transform]
...
*/

var Transform = function(element, style){
this.element = element;
this.style = style;
}; Transform.prototype = {

set: function(def){
this.element.style[this.style] = def;
return this;
},

clear: function(){
return this.set('');
},

regexes: {
'rotateX': /rotateX\((-?[0-9]+deg)\)/,
'rotateY': /rotateY\((-?[0-9]+deg)\)/,
'rotateZ': /rotateZ\((-?[0-9]+deg)\)/,
'translateX': /translateX\((-?[0-9]+%)\)/,
'translateY': /translateY\((-?[0-9]+%)\)/,
'translateZ': /translateZ\((-?[0-9]+px)\)/
},

add: function(rule, value){
var transform = this.element.style[this.style],
match = new RegExp(this.regexes[rule]).test(transform);
unit = new RegExp(/rotate/).test(rule)
? 'deg'
: new RegExp(/translateZ/).test(rule)
? 'px'
: '%',
shared = rule + '(' + value + unit +')';

if (transform == 'none') transform = '';
return match
? this.set(transform.replace(this.regexes[rule], shared))
: this.set(transform + ' ' + shared);
},

translate: function(axis, value){
if (typeof axis === 'string') return this.add('translate' + axis.toUpperCase(), value);
for (i in axis) if (axis.hasOwnProperty(i)) this.translate(i, axis[i])
return this;
},

rotate: function(axis, value){
if (typeof axis === 'string') return this.add('rotate' + axis.toUpperCase(), value);
for (i in axis) if (axis.hasOwnProperty(i)) this.rotate(i, axis[i])
return this;
}

};
71 changes: 71 additions & 0 deletions Tests/Transform/transform.html
@@ -0,0 +1,71 @@
<style>
#container {
width: 400px;
height: 280px;
position: absolute;
left: 440px;
top: 40px;
text-align: center;
-webkit-perspective: 800;
}
#transformer {
-webkit-transition: -webkit-transform 500ms ease-out;
width: 50%;
height: 50%;
background: #ccc;
line-height: 100px;
font-size: 25px;
}
</style>

<div id=container>
<div id=transformer>Transform me!</div>
</div>

<script src="/depender/build?require=Transform/Transform,Core/Element.Event"></script>

<script>
var transform = new Transform(document.getElementById('transformer'), 'WebkitTransform');

makeActions([
{
fn: function(){
transform.set('translateX(50%)');
},
title: "set('translateX(50%)')"
},
{
fn: function(){
transform.translate('y', 50);
},
title: "transform.translate('y', 50)"
},
{
fn: function(){
transform.rotate({x: 30, y: 30});
},
title: "transform.rotate({x: 30, y: 30})"
},
{
fn: function(){
transform.translate('z', -100);
},
title: "transform.translate('z', -100)"
},
{
fn: function(){
transform.translate('Z', 0);
},
title: "transform.translate('Z', 0)",
description: 'The last action defined translateZ, this action should just replace it, leaving the others in tact.'
},
{
fn: function(){
transform.clear();
},
title: 'clear'
}
]);


</script>
10 changes: 10 additions & 0 deletions package.yml
@@ -0,0 +1,10 @@
name: Transform

author: Ryan Florence

copyright: "&copy; [Ryan Florence](http://ryanflorence.com)"

license: MIT-style license.

sources:
- "Source/Transform.js"

0 comments on commit b24e005

Please sign in to comment.