2013-02-05 31 views
7

tôi là tạo ra một tập tin d.ts cho webgl-utils.js từ googlelàm thế nào tôi sẽ khai báo một 'khỉ vá' nguyên mẫu trong nguyên cảo

Tôi có một vấn đề với một trong những dòng cuối cùng mà một phương pháp trong một đối tượng toàn cầu là 'khỉ vá' (tôi nghĩ rằng đây là thuật ngữ bên phải)

dòng vấn đề đọc:

/** 
    * 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); 
      }; 
})(); 

Làm thế nào tôi có thể tuyên bố này trong tập tin nguyên cảo của tôi vì vậy tôi sẽ không nhận được biên dịch lỗi khi tôi sử dụng chức năng:

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

bây giờ tôi đã cố gắng:

interface window 
{ 
     requestAnimFrame(): any; 
} 

Nhưng điều này không loại bỏ các lỗi:

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

Bạn đã cố gắng đặt tiền tố một cách rõ ràng với 'window.'? – Bergi

+0

Có, điều đó cũng cung cấp lỗi giống hệt nhau – Toad

+0

, phần intellisense trong VisStudio không hiển thị phương thức. Nó hiển thị bình thường: requestAnimationFrame() nhưng không hiển thị thông tin mới – Toad

Trả lời

7

Bạn đang trên đường đi đúng hướng, nhưng bạn cần phải xác định tất cả các biến thể bạn có:

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

tuyệt vời. hoạt động hoàn hảo ngay bây giờ. – Toad

+1

Thao tác này có mở rộng giao diện 'Cửa sổ' trong' lib.d.ts' hoặc che dấu không? Kinh nghiệm của tôi cho thấy cách tiếp cận này ẩn các chức năng khác như 'window.setInterval'. –

+1

Giao diện được mở bằng TypeScript, miễn là bạn thêm 'cửa sổ giao diện thứ hai 'vào cùng một gốc chung (ở cùng cấp độ logic) như bản gốc, nó sẽ thêm vào giao diện hiện có. Với giao diện toàn cầu, nó thực sự dễ dàng để đảm bảo nó có cùng một gốc chung :) – Fenton

3

chắc chắn tên giao diện khởi động với vốn "W" không phải là "w"

interface Window { 
    requestAnimFrame():any; 
} 

Trong mã gọi sử dụng window.requestAnimFrame();. Hy vọng điều này sẽ giải quyết vấn đề của bạn

1

cách khác:

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

Cách duy nhất đã làm việc cho tôi:

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); 
     }; 
}