Canvas kéo và thả
Nếu ứng dụng của bạn có một sự nhấn mạnh đồ họa, sau đó lựa chọn để sử dụng vải có lẽ là một hợp lý. Trong khi các ứng dụng đồ họa có thể xảy ra với các phương pháp tiếp cận dựa trên DOM, như nhiều yếu tố đồ họa được thêm vào, DOM có thể trở nên lộn xộn và hiệu suất có thể bị ảnh hưởng. Kể từ khi vải được chỉ đơn giản là kết xuất như một bitmap, nó không có các chi phí tương tự. Điều này xuất phát ở một mức giá tuy nhiên. Vì không có các yếu tố thực tế, chỉ bitmap vải, không có xử lý sự kiện API, vì vậy điều này phải được quản lý bởi các ứng dụng.
Có nói này, chúng ta có thể kéo một yếu tố xung quanh trên vải nhiều như chúng tôi có thể kéo một yếu tố xung quanh trên trang, nhưng chúng tôi có một ít công việc để làm vì thiếu một sự kiện xử lý API. Kéo và thả trên một phần tử DOM là dễ dàng hơn, vì chúng ta có thể phát hiện các yếu tố, trong đó sự kiện xảy ra.
Với canvas, chúng ta sẽ biết được tọa độ nơi một liên lạc bắt đầu hoặc kết thúc, nhưng chúng ta cần phải xác định những đối tượng trên vải có thể có được mục tiêu tiềm năng của sự tương tác. Một cách tiếp cận đơn giản có thể duy trì một danh sách các đối tượng, và các khung giới hạn của họ, và cho mỗi sự kiện liên lạc, để phù hợp với danh sách này, để tìm thấy những gì đối tượng đã được hưởng, và để làm cho nó trở thành mục tiêu của các liên lạc. Đây là cách tiếp cận chúng ta lấy trong ví dụ sau:
Đơn giản Canvas kéo và thả dụ
Trong ví dụ này, chúng ta thấy làm thế nào để thực hiện một đơn giản kéo và thả trên canvas. Đầu tiên chúng ta bắt đầu với các phần tử canvas.
đang được dịch, vui lòng đợi..