Categories
discuss

Draw a circle filled with random color sqares on canvas

I’ve got pretty strange example to work on… I need to fill a circle with 1×1 pixels, all with different colors in a browser.

What I tried, is something like this

function getRandomColor() {
    var letters = '0123456789ABCDEF'.split('');
    var color = '#';
    for (var i = 0; i < 6; i++ ) {
        color += letters[Math.round(Math.random() * 15)];
    }
    return color;
}

function createRandomSqaure(destination) {
    var size = destination.height() * destination.width();

    for (var i = 0; i < size; i += 1) {
        destination.append('<div class="pixel" style="background: ' + getRandomColor() + '"></div>');
    }
}

createRandomSqaure($('.pic'));

The case is, it super slow (as you can imagine, loop goes 40k times for 200×200 image), I thought maybe a better way will be drawing It on canvas? And I need to draw a circle filled with this pixels in the end…

I don’t know how to do something like this in a more optimal way, I can use a nodejs server also, but I think generating something like this server side on heroku is a way too much.

I’m just curious what do you guys think, and what is the best way to do something like this?

Answer

You can use a simple approach like this:

  • Draw all the pixels with random colors in a 200×200 grid on a canvas
  • Change composite mode
  • Draw circle on top

Live demo

Results in:

enter image description here

Example:

var canvas = document.getElementById('canvas'), // get canvas element
    ctx = canvas.getContext('2d'),              // get context
    x, y = 0,                                   // initialize
    dia = canvas.width,
    radius = dia * 0.5;

ctx.translate(0.5, 0.5);                        // to make pixels sharper

for(; y < dia; y++) {                           // walk x/y grid
    for(x = 0; x < dia; x++) {
        ctx.fillStyle = getRndColor();          // set random color
        ctx.fillRect(x, y, 1, 1);               // draw a pixel
    }
}

// create circle

// removes pixels outside next shape
ctx.globalCompositeOperation = 'destination-in'; 
ctx.arc(radius, radius, radius, 0, 2*Math.PI);
ctx.fill();

// reset
ctx.globalCompositeOperation = 'source-over'; 

function getRndColor() {
    var r = 255*Math.random()|0,
        g = 255*Math.random()|0,
        b = 255*Math.random()|0;
    return 'rgb(' + r + ',' + g + ',' + b + ')';
}
Categories
discuss

LG G2 vs980 not recognized by ADB devices command (Win7 on virtualbox)

I have been developing android mobile apps via eclipse in school for some time now, deploying the apps to virtual devices. However, I now want to deploy them to my LG G2 android device for testing and speed purposes. I know there are posts about other devices not working, but the solutions are to no avail for me.

I am on virtualbox running windows 7. I have the necessary vb extensions installed and vb DOES detect my LG G2 (asks to view folders..etc), however eclipse/adb on my vb cannot find it still.

I have put what I thought is the proper device vendor id (1004, 0232) in the adb_usb.ini file since some others found that to help get their device detected.

I have done the commands adb kill-server & start-server many times. I have restarted the phone and vb also. I have also tried all of the phones connection types (internet/ethernet, MTP)

Some help would be awesome,
Thanks fellas

Answer

When you plug the phone in, you are given options for the “USB connection method”.

You must select Internet connection or Send Images (PTP) from the list in order to use USB Debugging tools.

For selecting Internet Connection, the other options sometimes include Charge phone, Media sync, and Camera.

For selecting Send Images (PTP), the other options sometimes include Charge Phone and Media Device (MTP).

Categories
discuss

How to load jQuery into WordPress properly

I have a WordPress site that I would like to use a jQuery plugin called DataTables. I’ve been having troubling figuring out the right way to load the jQuery script and the DataTables script into WordPress.

I know I’m supposed to use something with: wp_enqueue_script("jquery") – but I do not know where to put it or how to load the other jQuery plugin i need.

The last bit I tried was putting this in the header.php file for my WordPress site:

<?php wp_enqueue_script("jquery"); ?>

<?php wp_head(); ?>

Any help would be greatly appreciated!

Answer

jQuery is included in WordPress core and when you include your javascript, you can declare that your script is dependent on jquery being loaded as the third parameter of wp_enqueue_script: http://codex.wordpress.org/Function_Reference/wp_enqueue_script.

The proper way to use wp_enqueue_script is to include it in a callback that is attached to a specific action hook. That sounded incredibly complicated to me when I started with WordPress, but it’s actually a really good system because it allows you to specify when your code should run during the initialization of WordPress.

So, in your functions.php or a plugin, you add your hook.

add_action( 'wp_enqueue_scripts', 'my_js_include_function' );

Then, in that function, you running the actual inclusion, and declare that your script is dependent on jquery.

function my_js_include_function() {
    wp_enqueue_script( 'my_script.js', '/path/to/myscript.js', array('jquery') );
}

The first parameter of wp_ennqueue_script() is an arbitrary name you are assigning to your script. If you were to then declare another script was dependent on the original one, that’s how you would refer to it in the third parameter.

Categories
discuss

assign null default value for optional query param in route – Play Framework

I’m trying to define an optional query parameter that will map to a Long, but will be null when it is not present in the URL:

GET  /foo  controller.Foo.index(id: Long ?= null)

… and I essentially want to check if it was passed in or not:

public static Result index(Long id) {
    if (id == null) {...}
    ...
}

However, I’m getting a compilation error:

type mismatch; found : Null(null) required: Long Note that implicit conversions are not applicable because they are ambiguous: both method Long2longNullConflict in class LowPriorityImplicits of type (x: Null)Long and method Long2long in object Predef of type (x: Long)Long are possible conversion functions from Null(null) to Long

Why can’t I do this, assigning null to be a default value for an expected Long optional query parameter? What’s an alternative way to do this?

Answer

Remember that the optional query parameter in your route is of type scala.Long, not java.lang.Long. Scala’s Long type is equivalent to Java’s primitive long, and cannot be assigned a value of null.

Changing id to be of type java.lang.Long should fix the compilation error, and is perhaps the simplest way to resolve your issue:

GET  /foo  controller.Foo.index(id: java.lang.Long ?= null)

You could also try wrapping id in a Scala Option, seeing as this is the recommended way in Scala of handling optional values. However I don’t think that Play will map an optional Scala Long to an optional Java Long (or vice versa). You’ll either have to have a Java type in your route:

GET  /foo  controller.Foo.index(id: Option[java.lang.Long])

public static Result index(final Option<Long> id) {
    if (!id.isDefined()) {...}
    ...
}

Or a Scala type in your Java code:

GET  /foo  controller.Foo.index(id: Option[Long])

public static Result index(final Option<scala.Long> id) {
    if (!id.isDefined()) {...}
    ...
}
Categories
discuss

Square’s Retrofit Android: Hash With Contents of Request

I want to create a hash with some parts of my request using Square’s Retrofit library. The RequestInterceptor does not help me because it does not provide information about the request, it just makes possible to add information to it. I need to access the HTTP verb, all the headers and the REST path to create the hash. The hash would be added to the Authorization header. Any ideas?

Answer

In order to accomplish this with Retrofit 1.9.0 the only way is to use OkHttp interceptors (https://github.com/square/okhttp/wiki/Interceptors). The following code uses OkHttp 2.2.0:

    public class YourInterceptor implements Interceptor {


    @Override public Response intercept(Chain chain) throws IOException {

        Request request = chain.request();

        if (request != null) {
            RequestBody body = request.body();
            URL requestURL = request.url();
            String method = request.method();
            Headers headers = request.headers();

            Request.Builder signedRequestBuilder = request.newBuilder();
            signedRequestBuilder.addHeader("Authorization", "Your Signature");
            request = signedRequestBuilder.build();
        }
        return chain.proceed(request);
    }
}

. . .

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(new YourInterceptor());
RestAdapter restAdapter = new RestAdapter.Builder()
    .setClient(new OkClient(okHttpClient))
    .build();

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..