Skip to content

Commit

Permalink
implement CAMediaTimingFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
magicien committed Mar 13, 2017
1 parent 82e009f commit 8ed7e14
Show file tree
Hide file tree
Showing 37 changed files with 1,072 additions and 1,194 deletions.
1,657 changes: 774 additions & 883 deletions index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/js/CoreGraphics/CGPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import CGPoint from './CGPoint'
import CGPathFillRule from './CGPathFillRule'
import CGPathApplierFunction from './CGPathApplierFunction'

let _typeID = null
const _typeID = null

/**
* An immutable graphics path: a mathematical description of shapes or lines to be drawn in a graphics context.
Expand Down
4 changes: 2 additions & 2 deletions src/js/CoreGraphics/CGRect.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export default class CGRect {
*/
get debugDescription() {
if(this.size === null){
return `{null}`
return '{null}'
}
const origin = this.origin ? this.origin.debugDescription() : '{null}'
const size = this.size ? this.size.debugDescription() : '{null}'
Expand Down Expand Up @@ -412,7 +412,7 @@ export default class CGRect {
* @param {number} y -
* @param {number} width -
* @param {number} height -
* @returns {CGRect}
* @returns {CGRect} -
*/
static rectWithXYWidthHeight(x, y, width, height) {
const point = new CGPoint(x, y)
Expand Down
6 changes: 2 additions & 4 deletions src/js/ObjectiveC/NSObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import CGPoint from '../CoreGraphics/CGPoint'
import CGRect from '../CoreGraphics/CGRect'

let _accessInstanceVariablesDirectly = false

/**
* The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.
* @access public
Expand Down Expand Up @@ -895,7 +893,7 @@ Typically, however, you are encouraged to relinquish resources prior to finaliza
* @see https://developer.apple.com/reference/objectivec/nsobject/1415307-accessinstancevariablesdirectly
*/
static get accessInstanceVariablesDirectly() {
return _accessInstanceVariablesDirectly
return true
}
/**
*
Expand Down Expand Up @@ -1227,7 +1225,7 @@ You call this method in a try expression and handle any errors in the catch clau
if(typeof key !== 'string'){
throw 'error: valueForKey(key): key should be string'
}
if(this[key] === undefined){
if(typeof this[key] === 'undefined'){
return this.valueForUndefinedKey(key)
}
return this[key]
Expand Down
4 changes: 2 additions & 2 deletions src/js/QuartzCore/CAAnimation.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export default class CAAnimation extends NSObject {

/**
* @access public
* @returns {CAAnimation}
* @returns {CAAnimation} -
*/
copy() {
const anim = new CAAnimation()
Expand Down Expand Up @@ -235,7 +235,7 @@ export default class CAAnimation extends NSObject {
const baseTime = this._basetimeFromActivetime(activeTime)
let t = baseTime
if(this.timingFunction !== null){
t = this.timingFunction(baseTime)
t = this.timingFunction._getValueAtTime(baseTime)
}
this._handleEvents(obj, t)
}
Expand Down
5 changes: 3 additions & 2 deletions src/js/QuartzCore/CABasicAnimation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default class CABasicAnimation extends CAPropertyAnimation {
/**
* constructor
* @access public
* @param {?string} path -
* @constructor
*/
constructor(path) {
Expand Down Expand Up @@ -50,7 +51,7 @@ export default class CABasicAnimation extends CAPropertyAnimation {

/**
* @access public
* @returns {CABasicAnimation}
* @returns {CABasicAnimation} -
*/
copy() {
const anim = new CABasicAnimation(this.keyPath)
Expand All @@ -70,7 +71,7 @@ export default class CABasicAnimation extends CAPropertyAnimation {
const activeTime = this._basetimeFromActivetime(time)
let t = activeTime
if(this.timingFunction !== null){
t = this.timingFunction(activeTime)
t = this.timingFunction._getValueAtTime(activeTime)
}
if(t < 0){
t = 0
Expand Down
67 changes: 62 additions & 5 deletions src/js/QuartzCore/CAMediaTimingFunction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

import NSObject from '../ObjectiveC/NSObject'

import * as Constants from '../constants'

/**
* A function that defines the pacing of an animation as a timing curve.
Expand All @@ -13,14 +13,48 @@ export default class CAMediaTimingFunction extends NSObject {

// Creating Timing Functions

/**
* Returns an initialized timing function modeled as a cubic Bézier curve using the specified control points.
* @access public
* @constructor
* @param {number} c1x - A floating point number representing the x position of the c1 control point.
* @param {number} c1y - A floating point number representing the y position of the c1 control point.
* @param {number} c2x - A floating point number representing the x position of the c2 control point.
* @param {number} c2y - A floating point number representing the y position of the c2 control point.
* @desc The end points of the Bézier curve are automatically set to (0.0,0.0) and (1.0,1.0). The control points defining the Bézier curve are: [(0.0,0.0), (c1x,c1y), (c2x,c2y), (1.0,1.0)].
* @see https://developer.apple.com/reference/quartzcore/camediatimingfunction/1522235-init
*/
constructor(c1x, c1y, c2x, c2y) {
super()

this._c1x = c1x
this._c1y = c1y
this._c2x = c2x
this._c2y = c2y
}


/**
* Creates and returns a new instance of CAMediaTimingFunction configured with the predefined timing function specified by name.
* @access public
* @param {string} name - The timing function to use as specified in Predefined Timing Functions.
* @returns {void}
* @returns {CAMediaTimingFunction} -
* @see https://developer.apple.com/reference/quartzcore/camediatimingfunction/1521979-init
*/
init(name) {
static functionWithName(name) {
switch(name){
case Constants.kCAMediaTimingFunctionLinear:
return new CAMediaTimingFunction(0.0, 0.0, 1.0, 1.0)
case Constants.kCAMediaTimingFunctionEaseIn:
return new CAMediaTimingFunction(0.42, 0.0, 1.0, 1.0)
case Constants.kCAMediaTimingFunctionEaseOut:
return new CAMediaTimingFunction(0.0, 0.0, 0.58, 1.0)
case Constants.kCAMediaTimingFunctionEaseInEaseOut:
return new CAMediaTimingFunction(0.42, 0.0, 0.58, 1.0)
case Constants.kCAMediaTimingFunctionDefault:
return new CAMediaTimingFunction(0.25, 0.1, 0.25, 1.0)
}
throw new Error(`CAMediaTimingFunction: unknown name: ${name}`)
}

/**
Expand All @@ -30,11 +64,12 @@ export default class CAMediaTimingFunction extends NSObject {
* @param {number} c1y - A floating point number representing the y position of the c1 control point.
* @param {number} c2x - A floating point number representing the x position of the c2 control point.
* @param {number} c2y - A floating point number representing the y position of the c2 control point.
* @returns {void}
* @returns {CAMediaTimingFunction}
* @desc The end points of the Bézier curve are automatically set to (0.0,0.0) and (1.0,1.0). The control points defining the Bézier curve are: [(0.0,0.0), (c1x,c1y), (c2x,c2y), (1.0,1.0)].
* @see https://developer.apple.com/reference/quartzcore/camediatimingfunction/1522235-init
*/
initControlPoints(c1x, c1y, c2x, c2y) {
static functionWithControlPoints(c1x, c1y, c2x, c2y) {
return new CAMediaTimingFunction(c1x, c1y, c2x, c2y)
}

// Accessing the Control Points
Expand All @@ -50,4 +85,26 @@ export default class CAMediaTimingFunction extends NSObject {
*/
getControlPointAtValues(idx, ptr) {
}

_getValueAtTime(time) {
let t0 = 0
let t1 = 1
let t = 0.5
let r = 0

for(let i=0; i<8; i++){
r = 1 - t
const tval = 3 * t * r * (this._c1x * r + this._c2x * t) + t * t * t
if(time > tval){
t0 = t
}else{
t1 = t
}
t = (t0 + t1) * 0.5
}
r = 1 - t
const val = 3 * t * r * (this._c1y * r + this._c2y * t) + t * t * t

return val
}
}
4 changes: 2 additions & 2 deletions src/js/QuartzCore/CAPropertyAnimation.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class CAPropertyAnimation extends CAAnimation {

/**
* @access public
* @returns {CAPropertyAnimation}
* @returns {CAPropertyAnimation} -
*/
copy() {
const anim = new CAPropertyAnimation(this.keyPath)
Expand All @@ -81,7 +81,7 @@ export default class CAPropertyAnimation extends CAAnimation {
const activeTime = this._basetimeFromActivetime(time)
let t = activeTime
if(this.timingFunction !== null){
t = this.timingFunction(activeTime)
t = this.timingFunction._getValueAtTime(activeTime)
}
const value = this.valueFunction(t)
this._applyValue(obj, value)
Expand Down
4 changes: 1 addition & 3 deletions src/js/QuartzCore/CATransform3D.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'



/**
* Defines the standard transform matrix used throughout Core Animation.
* @access public
Expand All @@ -18,7 +16,7 @@ export default class CATransform3D {
* @returns {void}
* @see https://developer.apple.com/reference/quartzcore/catransform3d/1523734-init
*/
init(m) {
constructor(m) {

// Instance Properties

Expand Down
3 changes: 1 addition & 2 deletions src/js/SceneKit/SCNActionTimingFunction.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'


/**
* The signature for a block that manages animation timing, used by the timingFunction property.
* @type {function(time: number): number}
* @param {number} time - A fraction of the action’s The input value for the timing function, as determined by the timingMode property and the action’s current progress.
* @returns {number}
* @returns {number} -
* @desc Your block must return a floating-point value between 0.0 and 1.0, where 0.0 represents the starting state of the action’s animation and 1.0 represents the end state.
* @see https://developer.apple.com/reference/scenekit/scnactiontimingfunction
*/
Expand Down
4 changes: 2 additions & 2 deletions src/js/SceneKit/SCNBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ export default class SCNBox extends SCNGeometry {
}

_createGeometry() {
let sourceData = []
let indexData = []
const sourceData = []
const indexData = []

const left = -this.width * 0.5
const right = this.width * 0.5
Expand Down
2 changes: 1 addition & 1 deletion src/js/SceneKit/SCNCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export default class SCNCamera extends NSObject {

/**
* @access private
* @param {CGRect} viewRect
* @param {CGRect} viewRect -
* @returns {void}
*/
_updateProjectionTransform(viewRect) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/SceneKit/SCNFieldForceEvaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SCNVector3 from './SCNVector3'
* @param {number} mass - The mass of the object affected by the field. (See the mass property for physics bodies and the particleMass property for particle systems.)
* @param {number} charge - The electrical charge of the object affected by the field. (See the charge property for physics bodies and the particleCharge property for particle systems.)
* @param {number} time - The elapsed time, in seconds, since the last simulation step.
* @returns {SCNVector3}
* @returns {SCNVector3} -
* @desc Your block uses these parameters to compute and return an SCNVector3 force vector, which SceneKit then applies to the object affected by the field.
* @see https://developer.apple.com/reference/scenekit/scnfieldforceevaluator
*/
Expand Down
12 changes: 6 additions & 6 deletions src/js/SceneKit/SCNGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export default class SCNGeometry extends NSObject {
super()

if(!Array.isArray(sources)){
throw 'SCNGeometry(sources, elements): sources must be Array'
throw new Error('SCNGeometry(sources, elements): sources must be Array')
}
if(!Array.isArray(elements)){
throw 'SCNGeometry(sources, elements): elements must be Array'
throw new Error('SCNGeometry(sources, elements): elements must be Array')
}

// Managing Geometry Attributes
Expand Down Expand Up @@ -462,7 +462,7 @@ This method is for OpenGL shader programs only. To bind custom variable data for

this._vertexBuffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, this._vertexBuffer)
let arr = []
const arr = []
const vertexSource = this.getGeometrySourcesForSemantic(SCNGeometrySource.Semantic.vertex)[0]
const normalSource = this.getGeometrySourcesForSemantic(SCNGeometrySource.Semantic.normal)[0]
const texcoordSource = this.getGeometrySourcesForSemantic(SCNGeometrySource.Semantic.texcoord)[0]
Expand All @@ -471,13 +471,13 @@ This method is for OpenGL shader programs only. To bind custom variable data for
const vectorCount = vertexSource.vectorCount

if(vertexSource === undefined){
throw new Error(`vertexSource is undefined`)
throw new Error('vertexSource is undefined')
}
if(normalSource !== undefined && normalSource.vectorCount !== vectorCount){
throw new Error(`normalSource.vectorCount !== vertexSource.vectorCount`)
throw new Error('normalSource.vectorCount !== vertexSource.vectorCount')
}
if(texcoordSource !== undefined && texcoordSource.vectorCount !== vectorCount){
throw new Error(`texcoordSource.vectorCount !== vertexSource.vectorCount`)
throw new Error('texcoordSource.vectorCount !== vertexSource.vectorCount')
}

const vertexArray = vertexSource ? vertexSource.data : null
Expand Down
17 changes: 9 additions & 8 deletions src/js/SceneKit/SCNGeometrySource.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default class SCNGeometrySource extends NSObject {
}

/**
* @access pricate
* @access private
* @returns {boolean}
*/
_hasTypedArrayData() {
Expand Down Expand Up @@ -130,7 +130,7 @@ export default class SCNGeometrySource extends NSObject {
* @param {number} bytesPerComponent - The size, in bytes, of each vector component.
* @param {number} dataOffset - The offset, in bytes, from the beginning of the data to the first vector component to be used in the geometry source.
* @param {number} dataStride - The number of bytes from each vector to the next in the data.
* @returns {SCNGeometrySource}
* @returns {SCNGeometrySource} -
* @desc A geometry source’s data is an array of vectors, each of which represents a particular attribute (or semantic) of a vertex in the geometry. The other parameters determine how SceneKit interprets this data. For example, an array of vertex positions may have three 32-bit floating-point components per vector, but an array of texture coordinates may have two 8-bit integer coponents per vector. You can use the offset and stride parameters together to interleave data for multiple geometry sources in the same array, improving rendering performance. See SCNGeometrySource for details.To create a custom SCNGeometry object from the geometry source, use the init(sources:elements:) method.
* @see https://developer.apple.com/reference/scenekit/scngeometrysource/1523320-init
*/
Expand All @@ -154,7 +154,7 @@ export default class SCNGeometrySource extends NSObject {
* @access public
* @param {SCNVector3[]} vertices - An array of three-component vectors, each of which represents a vertex position for the geometry source.
* @param {number} count - The number of vertices
* @returns {SCNGeometrySource}
* @returns {SCNGeometrySource} -
* @desc SceneKit converts this data to its own format to optimize rendering performance. To read the converted data, examine the properties of the created SCNGeometrySource object.To create a custom SCNGeometry object from the geometry source, use the init(sources:elements:) method.
* @see https://developer.apple.com/reference/scenekit/scngeometrysource/2034708-init
*/
Expand Down Expand Up @@ -182,7 +182,7 @@ export default class SCNGeometrySource extends NSObject {
* @access public
* @param {CGPoint[]} texcoord - An array of points, each of which represents a texture coordinate pair for the geometry source.
* @param {number} count - The number of texture coordinate points.
* @returns {SCNGeometrySource}
* @returns {SCNGeometrySource} -
* @desc SceneKit converts this data to its own format to optimize rendering performance. To read the converted data, examine the properties of the created SCNGeometrySource object.To create a custom SCNGeometry object from the geometry source, use the init(sources:elements:) method.
* @see https://developer.apple.com/reference/scenekit/scngeometrysource/1522718-init
*/
Expand Down Expand Up @@ -211,7 +211,7 @@ export default class SCNGeometrySource extends NSObject {
* @access public
* @param {SCNVector3[]} normals - An array of vectors, which represents a normal vector for the geometry source.
* @param {number} count - The number of normals
* @returns {SCNGeometrySource}
* @returns {SCNGeometrySource} -
*/
static geometrySourceWithNormalsCount(normals, count) {
const data = []
Expand Down Expand Up @@ -325,7 +325,7 @@ export default class SCNGeometrySource extends NSObject {
* @param {number} vertexCount - The number of vertices in the geometry source.
* @param {number} offset - The offset, in bytes, from the beginning of the data to the first vector component to be used in the geometry source.
* @param {number} stride - The number of bytes from each vector to the next in the data.
* @returns {SCNGeometrySource}
* @returns {SCNGeometrySource} -
* @desc Use this method to create a geometry source whose underlying data can be modified at render time by a Metal compute shader running on the GPU. To create a MTLBuffer object for use with a geometry source, use the device property of the SceneKit view (or other renderer) responsible for drawing your scene.// Create and fill a buffer.
id <MTLDevice> device = self.scnView.device;
self.geometryBuffer = [device newBufferWithBytes:myData length:myLength options:myOptions];
Expand Down Expand Up @@ -405,14 +405,15 @@ SCNGeometrySource *source = [SCNGeometrySource geometrySourceWithBuffer:buffer

/**
* @access public
* @returns {number[]}
* @param {number} index -
* @returns {number[]} -
*/
vectorAt(index) {
if(index < 0 || index >= this.vectorCount){
return null
}
const indexStride = this._dataStride / this._bytesPerComponent
let ind = index * indexStride + this._dataOffset / this._bytesPerComponent
const ind = index * indexStride + this._dataOffset / this._bytesPerComponent
const arr = []
for(let i=0; i<this._componentsPerVector; i++){
arr.push(this._data[ind + i])
Expand Down
Loading

0 comments on commit 8ed7e14

Please sign in to comment.