2013-02-05 14 views
7

Sto creando un file d.tS per webgl-utils.js da GoogleCome potrei dichiarare un 'scimmia patchata' prototipo nel dattiloscritto

Ho un problema con una delle ultime linee in cui un metodo in un oggetto globale è 'scimmia patch' (credo che questo sia la terminologia destra)

la linea problema si legge:

/** 
    * Provides requestAnimationFrame in a cross browser way. 
    */ 
window.requestAnimFrame = (function() { 
    return window.requestAnimationFrame || 
      window.webkitRequestAnimationFrame || 
      window.mozRequestAnimationFrame || 
      window.oRequestAnimationFrame || 
      window.msRequestAnimationFrame || 
      function(/* function FrameRequestCallback */ callback, /* DOMElement Element */ element) { 
      window.setTimeout(callback, 1000/60); 
      }; 
})(); 

Come dovrei dichiarare il presente nel mio file dattiloscritto in modo da non otterrà errori di compilazione quando uso il funzione:

function tick() 
{ 
     requestAnimFrame(tick); 
     drawScene(); 
} 

ora ho provato:

interface window 
{ 
     requestAnimFrame(): any; 
} 

Ma questo non elimina l'errore:

The name 'requestAnimFrame' does not exist in the current scope 
+0

Hai provato a prefissarlo esplicitamente con 'window'? – Bergi

+0

Sì, che dà l'identico errore – Toad

+0

anche, l'intellisense in VisStudio non mostra il metodo. Mostra il normale: requestAnimationFrame() ma non quello nuovo – Toad

risposta

7

Si stavano dirigendo nella giusta direzione, ma è necessario definire tutte le variazioni si dispone:

interface Window { 
    requestAnimFrame(callback: any, element?: any): void; 
    webkitRequestAnimationFrame(callback: any, element?: any): void; 
    mozRequestAnimationFrame(callback: any, element?: any): void; 
    oRequestAnimationFrame(callback: any, element?: any): void; 
} 

window.requestAnimFrame = (function() { 
    return window.requestAnimationFrame || 
     window.webkitRequestAnimationFrame || 
     window.mozRequestAnimationFrame || 
     window.oRequestAnimationFrame || 
     window.msRequestAnimationFrame || 
     function(callback, element?) { 
      window.setTimeout(callback, 1000/60); 
     }; 
})(); 

function tick() { 
    window.requestAnimFrame(tick); 
} 
+1

impressionante. funziona perfettamente ora. – Toad

+1

Estende l'interfaccia 'Window' in' lib.d.ts', o maschera? La mia esperienza suggerisce che questo approccio nasconde altre funzioni come 'window.setInterval'. –

+1

Le interfacce sono aperte in TypeScript, quindi se si aggiunge la seconda 'finestra di interfaccia' nella stessa radice comune (allo stesso livello logicamente) dell'originale, si aggiungerà all'interfaccia esistente. Con le interfacce globali, è davvero facile assicurarsi che abbia la stessa radice comune :) – Fenton

3

assicurarsi che il nome dell'interfaccia inizia con un capitale "W", non "w"

interface Window { 
    requestAnimFrame():any; 
} 

Nel codice chiamante utilizzare window.requestAnimFrame();. Spero che questo risolverà il vostro problema

1

un altro modo:

declare var wnd: { 
    requestAnimationFrame: any; 
    webkitRequestAnimationFrame: any; 
    mozRequestAnimationFrame: any; 
    oRequestAnimationFrame: any; 
    msRequestAnimationFrame: any; 
} 
var wnd = window; 

export var requestAnimFrame = (function() { 
    return wnd.requestAnimationFrame || 
      wnd.webkitRequestAnimationFrame || 
      wnd.mozRequestAnimationFrame || 
      wnd.oRequestAnimationFrame || 
      wnd.msRequestAnimationFrame || 
      function (/* function FrameRequestCallback */ callback, /* DOMElement Element */ element) { 
       window.setTimeout(callback, 1000/60); 
      }; 
})(); 
0

L'unico modo che ha funzionato per me:

declare global { 
    interface Window { 
     requestAnimFrame(callback:() => void): any; 
     webkitRequestAnimationFrame(callback:() => void): any; 
     mozRequestAnimationFrame(callback:() => void): any; 
     oRequestAnimationFrame(callback:() => void): any; 
    } 
} 

Window.prototype.requestAnimFrame = function() { 
    return window.requestAnimationFrame || 
     window.webkitRequestAnimationFrame || 
     window.mozRequestAnimationFrame || 
     window.oRequestAnimationFrame || 
     function (callback) { 
      window.setTimeout(callback, 1000/60); 
     }; 
} 
Problemi correlati