Random posts about coding

Mostly blogging about dart.

How to Communicate With Chrome.* API in Dart

| Comments

I had the privilege to attend Chrome Apps Accelerated MTV hackathon. The goal set out for my self was to create a bluetooth scanner via chrome.bluetooth api.

First I started off by reviewing the Chrome app developer guide. From a previous post, a patch for 7228 was needed at the time.

Second was too communicate with the js defined context which Chrome Apps execute in. Some setup ceremony is needed such as a manifest file and background window. Creating the manifest file was easy, mostly choosing permissions and adding the entry Javascript background entry.

For dart we will need to create the background javascript that loads our dart application.

I stumbled with the communications between dart generated javascript and js-interop for some time. The first part of the stumbling blocks was CSP (Content Security Policy) and how dart2js apps are loaded. Thanks to Vijay Menon and Renato Mangini, I was able to create successful call and callback from dart to javascript. This was non trivial since it required knowledge of dart.js and loading of dart_interop.js/js.dart.

The diff file below removes the reference to localStorage and replaces it with a map.

dart_interop.js and js.dart needed to be copied into the root folder of the project. Not sure exactly why this is, very possible that only dart_interop.js is needed since were compiling to js.

The first sanity check of being able to communicate with dart in a Chrome App was printing out the permissions from chrome.permissions.getAll

Google searches lead me to the wrong api docs for accessing the bluetooth device. Managed to land on chrome.experimental.bluetooth from extensions api, which is different from the Chrome Apps chrome.* api. Bug was filed about how easy it was to land on the wrong api pages.

Now that I’ve got the right context too loaded, calling the chrome.bluetooth api produced errors with bluetooth support on MacOSX. doh! Bluetooth is currently only supported on ChromeOS and Windows. This was not mentioned in any of the docs and bug was filed on that.

1
2
Error during bluetooth.isAvailable: This operation is not supported on your platform 
chromeHidden.handleResponse

At this point I was glad to be able to make calls on chrome.* api, in a following post I will go over a more complete sample from start to finish. Feel free to browse code and project structure bluetooth_example_chrome_app. Please note this is not a fully working sample and has bugs!

By the end of the hackathon I decided my best bet was to package something that did not rely on the chrome.* api so heavily. An ASCII camera capture app was created and demoed. The application accesses the client’s video input device and converts images capture to ascii images based on a ascii art formula. “videoCapture” permissions are required for accessing video input, this was set in the manifest file. The ASCII camera capture app code is available on the following branch chrome_app_example.

Turning Solar3d Into a Chrome App

| Comments

Ventured tonight with creating an installable Chrome App in #dartlang.

I was compiling to javascript and ran into problems with dart2js using unsafe evals, there not allowed in Chrome Apps.

unsafeevals

dart2js provides the ability to disallow them using dart2js --disallow-unsafe-eval.

After another try loading up the app some funky business was going on, filed a bug at dartbug and within minutes someone had looked at the issue and pointed me to a patch.

Luckily the way dart2js is laid out in the dart-sdk a patch could be applied without side effects. Great design by the dart2js team, reducing the ability of needing to build from bleeding_edge.

22:37:10-adam@Adams-MacBook-Air:/Applications/dart/dart-sdk
$ curl https://codereview.chromium.org/download/issue11491005_1.diff | patch -p2
patching file lib/_internal/compiler/implementation/js_backend/emitter.dart
patching file lib/_internal/compiler/implementation/js_backend/emitter_no_eval.dart

Note you must remove the snapshot build of dart2js. The snapshot build of dart2js providers faster load times by the VM.

23:14:57-adam@Adams-MacBook-Air:~/dart/solar3d/web
$ rm -rf /Applications/dart/dart-sdk/lib/_internal/compiler/implementation/dart2js.dart.snapshot

Rebuilt and reloaded the application and solar3d worked as a Chrome App!

00:47:16-adam@Adams-MacBook-Air:~/dart/solar3d/web
$ dart2js --disallow-unsafe-eval -osolar.dart.js solar.dart

unsafeevals

Your welcome to checkout the minior modifications of solar3d on github. Follow the instructions Getting Started with Chrome Apps on loading unpacked extentions.

How to Use CanvasElement.toDataURL in Dart

| Comments

When looking to create an ImageElement from a CanvasElement’s context, toDataURL is your friend. An example use case of this would be creating screen shots of a canvas after some routine has ended. Another could be taking a screen shot when a playing a game that was developed with context 2d or webgl. toDataURL returns a data: URL containing a representation of the image in the format specified by type (defaults to PNG).

The following example shows the use of canvas 2d for creating a jpeg image and inserting it into the body of a document. If you run this example you might notice the loss of the alpha channel, that would be a good reason to switch to png format when calling toDataURL.

Ways to Solve the N Doors N Passes Question

| Comments

Question

1000 doors in a row that are initially closed. 1000 passes on the doors. Each time you visit a door you toggle it. If open->close, if close->open. First time you visit every door, second time you visit every other door, third time you visit every 3rd door, etc.. until visiting all 1000 doors. How many doors are left open at the end? Which are open, which are closed? What is unique about the sequence left open?

Solutions

The O(n2) solution to the problem which presents interesting results in its output.

The O(n) solution that takes advantage of a known identity of perfect squares found in the problem.

Joining Solvr.io

Pleased to announce that I’m joining #solvr.io full time to work with Lars Tackmann in #dart. Solvr is dedicated for creating modern math tools on the web. We find dart as the perfect opportunity to accomplish our goals, please follow and sign up for an alpha release at http://solvr.io. I feel dartlang has the potential to succeed in commercial opportunities. That helped me in the choice of leaving casino industry for math education tools. A second driver for me is the web ecosystem is ever more important these days, it needs to be in every developer’s toolbox. Finally, math is fun!

I would like to thank all the open source #dartlang developers/hackers/friends for creating the rich community around dart. The community effort has been accelerating at a wonderful speed, the new frameworks and libraries keep on evolving with the dart language. That will help us all in the long haul. This is a great time for startups, smaller companies and enterprises to get in now and create what we want to use over the next years.   


One of the first libraries I will be working on is dart-bignum to help with development that depends on fast implementation of BigInteger across dart and dart2js.

adam@solvr.io
http://solvr.io

Joining Solvr.io

| Comments

Pleased to announce that I’m joining #solvr.io full time to work with Lars Tackmann in #dart. Solvr is dedicated for creating modern math tools on the web. We find dart as the perfect opportunity to accomplish our goals, please follow and sign up for an alpha release at http://solvr.io. I feel dartlang has the potential to succeed in commercial opportunities. That helped me in the choice of leaving casino industry for math education tools. A second driver for me is the web ecosystem is ever more important these days, it needs to be in every developer’s toolbox. Finally, math is fun!

I would like to thank all the open source #dartlang developers/hackers/friends for creating the rich community around dart. The community effort has been accelerating at a wonderful speed, the new frameworks and libraries keep on evolving with the dart language. That will help us all in the long haul. This is a great time for startups, smaller companies and enterprises to get in now and create what we want to use over the next years.   

One of the first libraries I will be working on is dart-bignum to help with development that depends on fast implementation of BigInteger across dart and dart2js.

adam@solvr.io http://solvr.io