Sunday, September 14, 2014
Star Wars: Commander
Monday, September 8, 2014
Dynamically Changing the Color of SVG Using an ASP.NET HttpHandler
I have an SVG file that I'm using as the background image of elements on the page (using sprites). I wanted to dynamically update the color of the shapes and paths when theming the application, but I could not find a way to do this using CSS when the SVG is loaded as a background image (I have heard that inline <svg> elements will inherit style rules).
The way I solved the issue was to use a custom HttpHandler in my ASP.NET application that allows me to replace parts of the SVG with other content. Here's the code I used:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
namespace MyNamespace
{
public class DynamicSVGHandler : IHttpHandler
{
public bool IsReusable
{
get
{
return true;
}
}
public void ProcessRequest( HttpContext context )
{
var request = context.Request;
var response = context.Response;
var file = context.Server.MapPath( Path.ChangeExtension( request.Url.LocalPath, ".svg" ) );
if( File.Exists( file ) )
{
var lastWrite = File.GetLastWriteTimeUtc( file );
response.Clear();
response.ContentType = "image/svg+xml";
response.Cache.SetExpires( DateTime.Now.AddMinutes( 5d ) );
response.Cache.SetCacheability( HttpCacheability.Public );
List<KeyValuePair<string, string>> replacements = new List<KeyValuePair<string, string>>();
string value;
foreach( string key in request.QueryString )
{
if( key.Length > 3 && !string.IsNullOrEmpty( value = request.QueryString[key] ) && value.Length > 3 )//Implement any custom validation here.
replacements.Add( new List<KeyValuePair<string, string>>( key, value ) );
}
if( replacements.Count > 0 )
{
string line;
using( StreamReader reader = new StreamReader( File.OpenRead( file ) ) )
{
while( (line = reader.ReadLine()) != null )
{
foreach( var replacement in replacements )
line = line.Replace( replacement.Key, replacement.Value );
response.Output.WriteLine( line );
}
}
}
else
response.WriteFile( file );
}
else
{
response.Clear();
response.StatusCode = 404;
response.End();
}
}
}
}
Make sure to register your handler in your web.config (This example is for IIS 7+, integrated mode and I used the extension .dsvg):
<configuration>
<system.webServer>
<handlers>
<add name="*.dsvg" path="*.dsvg" preCondition="integratedMode" verb="*" type="MyNamespace.DynamicSVGHandler, MyAssembly"/>
</handlers>
</system.webServer>
</configuration>
You can now reference any svg file in your application with the extension .dsvg instead of .svg and specify replacements in the URL (e.g. mysvg.dsvg?white=red to change "white" to "red").
In my SVG, I found it easiest to define the color using a style section rather than setting the fill of each shape, then add the class="svg-shape" to all the shapes.
Thursday, March 13, 2014
Changing the Color of Table and Column Names in SQL Server Management Studio 2012
If you've upgraded to SQL Server 2012 and used the new Management Studio, you've probably noticed that your table and column names are now a teal color that looks exactly like the color of your comments. Previous versions of Management Studio would use the color specified for "Plain Text", but the new version uses a new setting.
To change the color, open the Tools menu and choose Options.... In the Environment settings click Fonts and Colors. Locate and click "Identifier" in the Display Items list box then change the Item foreground to something other than Default. That should do the trick.
Monday, February 24, 2014
How to Restore Files from a Restore Point
In my last post, I mentioned how I had used Windows System Restore, only to find out that JavaScript (*.js) files are considered to be "system files" and are restored back to the state they existed when the restore point was created. I tried restoring back to the previous state, but no-go. System Restore failed with every other restore point.
I thought all was lost, until I found this article by the How-to Geek that has a batch file letting you mount the latest VSS (Volume Shadow Copy) volume and view the files contained there-in (when you create a Restore Point, Windows creates a VSS volume containing your files at that point in time). I didn't want the most recent volume, but I looked at the batch file and figured out how to list the available volumes and mount the one I wanted. I figured I'd document the steps in case this helps anyone else.
- First off, you need to open a command prompt as an administrator (Right-click the "Command Prompt" and choose "Run as Administrator").
- At the prompt, type
This will list the available VSS volumes on your system. This can be confusing, but each one displays a creation time. What you're looking for is the the "Shadow Copy Volume" (e.g. \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1).VSSAdmin List Shadows
- Once you find out the name of the shadow copy volumne, you can mount the volume using MKLINK just as you would any other folder (if you haven't used MKLINK, I'd recommend you check it out - it's a pretty useful tool). For example:
In this example, you'll see a new folder named ShadowBackup on your C: drive, containing the contents of the VSS volume.MKLINK /D C:\ShadowBackup \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy1
Now you can view the contents of the VSS volume and view/copy any of the files within. This can also be useful if you accidentally delete a file or want to see what changes you've made.
To "unmount" the volume, you can simply delete the folder in Windows Explorer. This won't delete the volume, just the link to view the files.
Warning: System Restore will Modify/Delete JavaScript Files!
I discovered too late that Windows System Restore considers JavaScript (*.js) files to be system files. Apparently, the way System Restore works is it uses the Volume Shadow Copy service to back up your files, then during the restore process, it restores "system" files to their state at the time the restore point was created.
As a developer, this is bad for me, since I regularly create and edit JS files. After restoring my system, I opened up my code and found, to my surprise that all my JS changes had been reverted. If this happens to you, in my next post, I'll detail how I managed to restore the files.
Tuesday, January 14, 2014
Munchkin Rules Removed by Request.
Monday, January 6, 2014
Performance of a shortcut to document.getElementById
I don't like typing document.getElementById
(or document.createElement
, etc.) repeatedly so for a while now, I've used my own function, I call byId (I know jQuery has $get, but I don't use jQuery, for reasons I won't go into here). This function started out as a simple:
function byId(id)
{
return document.getElementById(id);
}
In Internet Explorer, I could use var byId = document.getElementById
, but this didn't work in other browsers. I then tried, what I though was a more elegant solution of binding byId to the document object (var byId = document.getElementById.bind(document)
). This works, but it winds up being slower then then simple method in all browsers, but IE. I decided to publish my results on jsperf for anyone interested.
What's really maddening is that the best method is different in each browser. I'm sure any web developers have experienced this themselves.
Sneaky Christmas Music
More and more, it seems that every year the holidays sneak up on me and suddenly it's Christmas. I was thinking about it last night, and I think I figured out why.
For starters, I'm older and time seems to go faster as you get older because a defined period of time is a smaller and smaller percentage of your memories. But it's more than that. I think it's the music.
Back when I was younger, the radio was constantly playing Christmas music. We had to go outside to shop and all the stores were replaying Christmas songs (driving their employees crazy).
This year, I listened to very little Christmas music. Due to the digital age, I had Pandora, podcasts, and my iTunes music library. I rarely ever turn on the radio anymore. I think next year I might try and mix in a little holiday cheer to my auditory experience and see how that affects me.
Friday, January 3, 2014
Munchkin Rules in HTML Format
I love playing Munchkin so much, I decided to translate the rules of Munchkin into HTML format to make them more searchable/linkable/viewable on other devices and post them here as a good way to access them anywhere. Note that I am in no way affiliated with Steve Jackson Games and if they ask me to take these down, I will without hesitation (though I'd ask them to make them available on their own site).
*** Edit: Removed by request ***